Thursday, August 18, 2011

[android-developers] DexClassLoader and DexFile Leaking File Handles

I have an application that uses a DexClassLoader to load some
dynamically updateable plugin code. I created a stress test
essentially looks like this:

while (true) {
Copy plugin jar from assets to temp directory
Create a DexClassLoader using plugin jar as the classpath
Load plugin using this class loader
Delete plugin jar from temp directory
}

Eventually, creating the DexClassLoader fails due to lack of disk
space. When I look in /proc/<pid>/fd, I see that my process is still
holding file handles to all of the now-deleted copies of the plugin
jar in the temp directory. I see from looking at the code that
DexClassLoader has an array of JarFile's and an array of DexFile's,
which never seem to get closed. I created my own copy of
DexClassLoader that has a close() method that cleans up these arrays,
but this has two problems:
1. It is not obvious when I should call the close method, since it's
difficult to know when all of the objects from an instance of the
plugin are gone. I assume it's not safe to invalidate a classloader
when objects loaded from it are still around, since they might need to
load other classes from the classloader later.
2. Even when I do call the close method on my classloader, calling
DexFile.close() does not cause it to release it's handle to the jar
file. This is consistent with the documentation for DexFile.

Is there a good way to clean up the resources used by a
DexClassLoader? Or do I just have to hope that in the real world, my
application's process gets restarted often enough that I won't hit
this problem?

Thanks,
Matt

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment