【问题标题】:Android, memory overuse issueAndroid,内存过度使用问题
【发布时间】:2014-03-30 11:29:10
【问题描述】:
我是 Android 开发的初学者。我的应用程序中有几个屏幕,我可以使用“backstack”功能在这些屏幕之间来回导航。当我深入浏览应用程序时,应用程序的内存使用量正在增加。有时它会达到 100 MB ..!!它会导致应用重新启动。
据我了解,以下可能是内存过度使用的可能原因:
1) 应用大量使用图片。
2) 实现了Backstacking。 (活动只有一次,其他都是片段)
3) 该应用程序有一个持续运行的后台服务。
我不确定这些是否是实际原因。请帮我找出原因。关于内存过度使用问题的任何提示?还有一个问题,Android 应用理想的内存使用量应该是多少?
【问题讨论】:
标签:
android
memory-management
android-activity
back-stack
background-service
【解决方案1】:
在您的应用程序中调用这两个方法,它将从 android 数据中删除所有图像缓存,并且您的应用程序不会因内存问题而崩溃
//this is for clear cache folder in android -> data folder which generate by gallry lazy loading
public static void trimCache(Context context) {
try {
// File dir = context.getCacheDir();
File dir = context.getExternalCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
System.out.println("delete cache folderrrrrrrrrr");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void trimCacheinternal(Context context) {
try {
File dir = context.getCacheDir();
// File dir = context.getExternalCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
System.out.println("delete cache folderrrrrrrrrr");
}
} catch (Exception e) {
// TODO: handle exception
}
}