Attempt to reduce JarFileWrapper heap consumption

Update `JarURLConnection` to use a single shared wrapper per
jar file rather than creating a new one each time. This update
should help to reduce GC pressure.

Fixes gh-28042
This commit is contained in:
Phillip Webb 2021-10-14 20:22:07 -07:00
parent 8e704aab48
commit 7388f2b60b
2 changed files with 13 additions and 2 deletions

View File

@ -91,6 +91,8 @@ public class JarFile extends AbstractJarFile implements Iterable<java.util.jar.J
private volatile boolean closed;
private volatile JarFileWrapper wrapper;
/**
* Create a new {@link JarFile} backed by the specified file.
* @param file the root jar file
@ -183,6 +185,15 @@ public class JarFile extends AbstractJarFile implements Iterable<java.util.jar.J
};
}
JarFileWrapper getWrapper() throws IOException {
JarFileWrapper wrapper = this.wrapper;
if (wrapper == null) {
wrapper = new JarFileWrapper(this);
this.wrapper = wrapper;
}
return wrapper;
}
@Override
Permission getPermission() {
return new FilePermission(this.rootFile.getFile().getPath(), READ_ACTION);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -263,7 +263,7 @@ final class JarURLConnection extends java.net.JarURLConnection {
&& !jarFile.containsEntry(jarEntryName.toString())) {
return NOT_FOUND_CONNECTION;
}
return new JarURLConnection(url, new JarFileWrapper(jarFile), jarEntryName);
return new JarURLConnection(url, jarFile.getWrapper(), jarEntryName);
}
private static int indexOfRootSpec(StringSequence file, String pathFromRoot) {