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
2.4.x
Phillip Webb 3 years ago
parent 8e704aab48
commit 7388f2b60b

@ -91,6 +91,8 @@ public class JarFile extends AbstractJarFile implements Iterable<java.util.jar.J
private volatile boolean closed; private volatile boolean closed;
private volatile JarFileWrapper wrapper;
/** /**
* Create a new {@link JarFile} backed by the specified file. * Create a new {@link JarFile} backed by the specified file.
* @param file the root jar 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 @Override
Permission getPermission() { Permission getPermission() {
return new FilePermission(this.rootFile.getFile().getPath(), READ_ACTION); return new FilePermission(this.rootFile.getFile().getPath(), READ_ACTION);

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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())) { && !jarFile.containsEntry(jarEntryName.toString())) {
return NOT_FOUND_CONNECTION; 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) { private static int indexOfRootSpec(StringSequence file, String pathFromRoot) {

Loading…
Cancel
Save