More memory leaks recently and again from various byte-code manipulators. A memory dump showed huge swathes of memory taken by classes whose names indicate that they were dynamically generated by CGLIB.
The problem manifested itself during integration test runs where we constantly start and stop Jetty servers. Since we start and stop Jetty programatically (see a previous post) and the way we used Spring seemed to dynamically generate many classes, these classes ultimately consumed all the memory because they were then be stored in static references - that is, they are stored with the classes. And these classes were not garbage collected since Spring's classloader was the same as that running the test.
"Types loaded through the bootstrap class loader will always be reachable and will never be unloaded. Only types loaded through user-defined class loaders can become unreachable and can be unloaded by the virtual machine." - Inside the Java 2 Virtual Machine, Bill Venners, p266.
The problem manifested itself during integration test runs where we constantly start and stop Jetty servers. Since we start and stop Jetty programatically (see a previous post) and the way we used Spring seemed to dynamically generate many classes, these classes ultimately consumed all the memory because they were then be stored in static references - that is, they are stored with the classes. And these classes were not garbage collected since Spring's classloader was the same as that running the test.
"Types loaded through the bootstrap class loader will always be reachable and will never be unloaded. Only types loaded through user-defined class loaders can become unreachable and can be unloaded by the virtual machine." - Inside the Java 2 Virtual Machine, Bill Venners, p266.
If we'd deployed a WAR file presumably all generated classes would have been garbage collected when the web app was unloaded in the test's tear down method.
To demonstrate our problem, let's:
package com.henryp.classloader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MyClassLoader extends ClassLoader {
private final String baseDir;
MyClassLoader(String baseDir) {
super();
this.baseDir = baseDir;
}
@Override
protected Class findClass(String filename) throws ClassNotFoundException {
filename = filename.replace(".", "/") + ".class";
InputStream resourceAsStream = null;
try {
URL url = new URL(baseDir + filename);
System.out.println("URL = " + url);
resourceAsStream = url.openStream();
System.out.println("Resource null? " + (resourceAsStream == null));
byte[] buffer = new byte[4096];
byte[] bytes = new byte[4096];
int read = resourceAsStream.read(buffer);
int total = 0;
while (read != -1) { // this could be a lot more efficient but it does the job
System.out.println("Read " + read + " bytes");
byte[] dest = new byte[total + read];
System.arraycopy(bytes, 0, dest, 0, total);
System.arraycopy(buffer, 0, dest, total, read);
bytes = dest;
total += read;
read = resourceAsStream.read(buffer);
}
return defineClass(null, bytes, 0, total);
} catch (IOException e) {
e.printStackTrace();
throw new ClassNotFoundException(filename);
} finally {
try {
resourceAsStream.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
Not awfully pretty but it doesn't need to be terribly efficient.
The main class that uses our class loader looks like this (notice that we measure the memory before and after our class loader is eligible for garbage collection):
package com.henryp.classloader;
public class Main {
public static void main(String[] args) {
Main app = new Main();
app.doClassloading();
}
private void doClassloading() {
MyClassLoader classLoader = new MyClassLoader("file:/Users/phenry/Documents/workspace/TestOther/bin/");
consumeMemory(classLoader);
long beforeNull = gcThenLogMemory();
System.out.println("Making classloader eligible for garbage collection");
classLoader = null;
long afterNull = gcThenLogMemory();
System.out.println("Memory usage change: " + (afterNull - beforeNull) + " bytes liberated");
}
private void consumeMemory(MyClassLoader classLoader) {
try {
// filename from a project that depends on this but this project does not depend on that
Class clazz = classLoader.findClass("com.henryp.classloader.OtherStaticReferenceBloater");
consumeMemory(clazz);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
private static long gcThenLogMemory() {
System.gc();
long freeMemory = Runtime.getRuntime().freeMemory();
System.out.println("After garbage collection, free memory = " + freeMemory);
return freeMemory;
}
private void consumeMemory(Class clazz) throws Exception {
LargeMemoryConsumable memoryConsumer = instantiateMemoryConsumer(clazz);
long initialMemory = Runtime.getRuntime().freeMemory();
doCallOnForeignObject(memoryConsumer);
System.gc();
long finalMemory = Runtime.getRuntime().freeMemory();
System.out.println("Initial memory = " + initialMemory);
System.out.println("Final memory = " + finalMemory);
System.out.println("Total consumed = " + (initialMemory - finalMemory));
}
private LargeMemoryConsumable instantiateMemoryConsumer(Class clazz)
throws InstantiationException, IllegalAccessException {
System.out.println("Class name: " + clazz.getName());
gcThenLogMemory();
LargeMemoryConsumable memoryConsumer = (LargeMemoryConsumable) clazz.newInstance();
checkDifferentClassLoader(clazz);
return memoryConsumer;
}
private void doCallOnForeignObject(LargeMemoryConsumable memoryConsumer) throws Exception {
for (int i = 0 ; i < 500000 ; i++) {
memoryConsumer.consumeMemory();
}
}
private void checkDifferentClassLoader(Class clazz) {
if (clazz.getClassLoader() == this.getClass().getClassLoader()) {
System.out.println("Experiment useless if the classloaders are the same");
System.exit(-1);
}
}
}
And the memory consumer we refer to is a simple interface:
package com.henryp.classloader;
public interface LargeMemoryConsumable {
public void consumeMemory() throws Exception;
}
Now, this is where it gets a little more interesting. In another project, we have an object that just takes up memory. But note that although this new project depends on the classes in our first project, the first project does not reference this new project. It does, however, use a URL that points at the directory that happens to be the directory into which the classes of the new project are compiled.
(I've added a toString method to stop the compiler optimizing away the aString member.)
package com.henryp.classloader;
public class MyOtherFatObject {
public static Object create() {
return new MyOtherFatObject("" + System.currentTimeMillis());
}
private final String aString;
private MyOtherFatObject(String aString) {
super();
this.aString = aString;
}
@Override
public String toString() {
return "MyOtherFatObject [aString=" + aString + "]";
}
}
In the same project, we have a class that emulates what we saw Spring doing, that is having a static reference to a collection of memory-consuming objects.
package com.henryp.classloader;
import java.util.ArrayList;
import java.util.Collection;
public class OtherStaticReferenceBloater implements LargeMemoryConsumable {
private static final Collection
public void consumeMemory() throws Exception {
if (this.getClass().getClassLoader() != MyOtherFatObject.class.getClassLoader()) {
throw new Exception("Different classloaders");
}
objects.add(MyOtherFatObject.create());
}
public void addObject(Object object) {
objects.add(object);
}
}
Running with these JVM args:
-verbose:gc -Xmx64m -Xmn64m
URL = file:/Users/phenry/Documents/workspace/TestOther/bin/com/henryp/classloader/OtherStaticReferenceBloater.class
Resource null? false
Read 1300 bytes
Class name: com.henryp.classloader.OtherStaticReferenceBloater
After garbage collection, free memory = 63333768
URL = file:/Users/phenry/Documents/workspace/TestOther/bin/com/henryp/classloader/MyOtherFatObject.class
Resource null? false
Read 896 bytes
Initial memory = 63333768
Final memory = 15130184
Total consumed = 48203584
After garbage collection, free memory = 15130200
Making classloader eligible for garbage collection
After garbage collection, free memory = 66266816
Memory usage change: 51136616 bytes liberated
(Notice how we explicitly load one class but it implicitly pulls in another using our class loader, the same class loader that loaded it - see the lines beginning with URL = ...).
To demonstrate our problem, let's:
- write our own class loader
- use it to load a class
- instantiate an instance of that class and let it consume lots of memory by storing objects in one of its static references.
- allow the classloader to be garbage collected
- see how the memory changed during all these steps
package com.henryp.classloader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MyClassLoader extends ClassLoader {
private final String baseDir;
MyClassLoader(String baseDir) {
super();
this.baseDir = baseDir;
}
@Override
protected Class findClass(String filename) throws ClassNotFoundException {
filename = filename.replace(".", "/") + ".class";
InputStream resourceAsStream = null;
try {
URL url = new URL(baseDir + filename);
System.out.println("URL = " + url);
resourceAsStream = url.openStream();
System.out.println("Resource null? " + (resourceAsStream == null));
byte[] buffer = new byte[4096];
byte[] bytes = new byte[4096];
int read = resourceAsStream.read(buffer);
int total = 0;
while (read != -1) { // this could be a lot more efficient but it does the job
System.out.println("Read " + read + " bytes");
byte[] dest = new byte[total + read];
System.arraycopy(bytes, 0, dest, 0, total);
System.arraycopy(buffer, 0, dest, total, read);
bytes = dest;
total += read;
read = resourceAsStream.read(buffer);
}
return defineClass(null, bytes, 0, total);
} catch (IOException e) {
e.printStackTrace();
throw new ClassNotFoundException(filename);
} finally {
try {
resourceAsStream.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
Not awfully pretty but it doesn't need to be terribly efficient.
The main class that uses our class loader looks like this (notice that we measure the memory before and after our class loader is eligible for garbage collection):
package com.henryp.classloader;
public class Main {
public static void main(String[] args) {
Main app = new Main();
app.doClassloading();
}
private void doClassloading() {
MyClassLoader classLoader = new MyClassLoader("file:/Users/phenry/Documents/workspace/TestOther/bin/");
consumeMemory(classLoader);
long beforeNull = gcThenLogMemory();
System.out.println("Making classloader eligible for garbage collection");
classLoader = null;
long afterNull = gcThenLogMemory();
System.out.println("Memory usage change: " + (afterNull - beforeNull) + " bytes liberated");
}
private void consumeMemory(MyClassLoader classLoader) {
try {
// filename from a project that depends on this but this project does not depend on that
Class clazz = classLoader.findClass("com.henryp.classloader.OtherStaticReferenceBloater");
consumeMemory(clazz);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
private static long gcThenLogMemory() {
System.gc();
long freeMemory = Runtime.getRuntime().freeMemory();
System.out.println("After garbage collection, free memory = " + freeMemory);
return freeMemory;
}
private void consumeMemory(Class clazz) throws Exception {
LargeMemoryConsumable memoryConsumer = instantiateMemoryConsumer(clazz);
long initialMemory = Runtime.getRuntime().freeMemory();
doCallOnForeignObject(memoryConsumer);
System.gc();
long finalMemory = Runtime.getRuntime().freeMemory();
System.out.println("Initial memory = " + initialMemory);
System.out.println("Final memory = " + finalMemory);
System.out.println("Total consumed = " + (initialMemory - finalMemory));
}
private LargeMemoryConsumable instantiateMemoryConsumer(Class clazz)
throws InstantiationException, IllegalAccessException {
System.out.println("Class name: " + clazz.getName());
gcThenLogMemory();
LargeMemoryConsumable memoryConsumer = (LargeMemoryConsumable) clazz.newInstance();
checkDifferentClassLoader(clazz);
return memoryConsumer;
}
private void doCallOnForeignObject(LargeMemoryConsumable memoryConsumer) throws Exception {
for (int i = 0 ; i < 500000 ; i++) {
memoryConsumer.consumeMemory();
}
}
private void checkDifferentClassLoader(Class clazz) {
if (clazz.getClassLoader() == this.getClass().getClassLoader()) {
System.out.println("Experiment useless if the classloaders are the same");
System.exit(-1);
}
}
}
And the memory consumer we refer to is a simple interface:
package com.henryp.classloader;
public interface LargeMemoryConsumable {
public void consumeMemory() throws Exception;
}
Now, this is where it gets a little more interesting. In another project, we have an object that just takes up memory. But note that although this new project depends on the classes in our first project, the first project does not reference this new project. It does, however, use a URL that points at the directory that happens to be the directory into which the classes of the new project are compiled.
(I've added a toString method to stop the compiler optimizing away the aString member.)
package com.henryp.classloader;
public class MyOtherFatObject {
public static Object create() {
return new MyOtherFatObject("" + System.currentTimeMillis());
}
private final String aString;
private MyOtherFatObject(String aString) {
super();
this.aString = aString;
}
@Override
public String toString() {
return "MyOtherFatObject [aString=" + aString + "]";
}
}
In the same project, we have a class that emulates what we saw Spring doing, that is having a static reference to a collection of memory-consuming objects.
package com.henryp.classloader;
import java.util.ArrayList;
import java.util.Collection;
public class OtherStaticReferenceBloater implements LargeMemoryConsumable {
private static final Collection
public void consumeMemory() throws Exception {
if (this.getClass().getClassLoader() != MyOtherFatObject.class.getClassLoader()) {
throw new Exception("Different classloaders");
}
objects.add(MyOtherFatObject.create());
}
public void addObject(Object object) {
objects.add(object);
}
}
Running with these JVM args:
-verbose:gc -Xmx64m -Xmn64m
the output looks like:
URL = file:/Users/phenry/Documents/workspace/TestOther/bin/com/henryp/classloader/OtherStaticReferenceBloater.class
Resource null? false
Read 1300 bytes
Class name: com.henryp.classloader.OtherStaticReferenceBloater
After garbage collection, free memory = 63333768
URL = file:/Users/phenry/Documents/workspace/TestOther/bin/com/henryp/classloader/MyOtherFatObject.class
Resource null? false
Read 896 bytes
Initial memory = 63333768
Final memory = 15130184
Total consumed = 48203584
After garbage collection, free memory = 15130200
Making classloader eligible for garbage collection
After garbage collection, free memory = 66266816
Memory usage change: 51136616 bytes liberated
(Notice how we explicitly load one class but it implicitly pulls in another using our class loader, the same class loader that loaded it - see the lines beginning with URL = ...).
Noting the usual caveats that System.gc() is a suggestion to the JVM and not a guarantee that the garbage collector should run, making the reference to the class loader null seems to liberate all the objects stored in the static reference of the class it loaded.
What we finally did was make all references we had in our integration tests to Spring's context loaders non-static. As our tests were garbage collected, memory was freed.
What we finally did was make all references we had in our integration tests to Spring's context loaders non-static. As our tests were garbage collected, memory was freed.
No comments:
Post a Comment