emm*_*mby 9 android memory-leaks
我正在开发Android应用程序,我们正在调查内存使用情况.
从hprof查看堆转储,我们看到在JarURLConnectionImpl中的静态缓存中使用了近2M(我们的堆的22%):

查看JarURLConnectionImpl的源代码,看起来条目被添加到静态jarCache变量,但从未被删除.
如果它们永远不会被删除,那就会让我感到内存泄漏.
这是泄漏吗?有修复或解决方法吗?
这是一个丑陋的解决方法:
private static HashMap<URL,JarFile> jarCache;
static {
try {
Class<?> jarURLConnectionImplClass = Class.forName("org.apache.harmony.luni.internal.net.www.protocol.jar.JarURLConnectionImpl");
final Field jarCacheField = jarURLConnectionImplClass.getDeclaredField("jarCache");
jarCacheField.setAccessible(true);
//noinspection unchecked
jarCache = (HashMap<URL, JarFile>) jarCacheField.get(null);
} catch(Exception e) {
// ignored
}
}
Run Code Online (Sandbox Code Playgroud)
然后,定期运行以下内容:
// HACK http://stackoverflow.com/questions/14610350/android-memory-leak-in-apache-harmonys-jarurlconnectionimpl
if( jarCache!=null ) {
try {
for (
final Iterator<Map.Entry<URL, JarFile>> iterator = jarCache.entrySet().iterator(); iterator.hasNext(); ) {
final Map.Entry<URL, JarFile> e = iterator.next();
final URL url = e.getKey();
if (Strings.toString(url).endsWith(".apk")) {
Log.i(TAG,"Removing static hashmap entry for " + url);
try {
final JarFile jarFile = e.getValue();
jarFile.close();
iterator.remove();
} catch( Exception f ) {
Log.e(TAG,"Error removing hashmap entry for "+ url,f);
}
}
}
} catch( Exception e ) {
// ignored
}
}
Run Code Online (Sandbox Code Playgroud)
我在创建活动时运行它,因此每次创建一个活动时它都会被执行.丑陋的hashmap条目似乎不会经常重新创建,但它似乎偶尔会重新出现,因此仅运行此代码一次是不够的.
| 归档时间: |
|
| 查看次数: |
1463 次 |
| 最近记录: |