我一直在做一些研究,但我仍然不能 100% 确定这是否会导致基于 内存泄漏。我正在使用按钮 View (v.context)。我认为我很好,因为上下文没有存储为静态,但如果可能的话,我想要一些反馈。我看到的主要问题是 OSMonitor ... (M) 值不断上升。随着小部件的每次打开/关闭和屏幕旋转。
32M 43M 61M 77M 等等……
我不确定 (M) 是兆字节还是兆比特。如果这是基于堆栈,我假设 Megebits perhpas 因为大多数高端设备在堆栈上限制为 32/48 MB(或其他)。
感谢您的反馈/加倍关注!
这是市场中的横幅应用程序,顺便说一句......
public class Globals {
public static final String PREF_NAME = "BannerPreferences";
public static final int MAX_TEXT_SIZE = 20;
// refresh ALL widgets loaded on the user's screens
// this could be for removing or adding 'pendingIntents or during bootup
public static void refreshAllWidgets(Context context) {
Logger.d("BANNER", "Globals:refreshAllWidgets");
invalidateWidgets(context, BannerWidget.class); // 1x4
invalidateWidgets(context, BannerWidget1x2.class);
invalidateWidgets(context, BannerWidget2x2.class);
}
// there has to be a API way to do this!! Until then, just loop thru all
// widget_provider classes..
private static void invalidateWidgets(Context context, Class<?> cls) {
ComponentName comp = new ComponentName(context, cls);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(comp);
for (int i = 0; i < appWidgetIds.length; i++) {
BannerWidgetBase.updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
}
appWidgetIds = null;
}
最佳答案
不一定有泄漏。由于 Dalvik VM 的性质,堆在使用时会不断增长,直到达到最大堆大小。但是,堆中可能有足够的空间用于您的对象。我建议限制模拟器镜像中的进程内存(堆),看看你是否真的遇到了 OutOfMemoryError。创建模拟器时,有一个属性“Max VM application heap size”要设置,例如到 32(以兆字节为单位)。
如果遇到 OutOfMemoryError,您应该仔细查看 Eclipse MAT。
P.S.:刚刚意识到您可能应该在您的案例中使用应用程序上下文,而不是 Activity 。如果您从 Activity 中触发它,请考虑 getApplicationContext而不是将 Activity 作为上下文传递。静态内容可能比 Activity 实例存在时间更长。
关于android - 上下文的静态内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8421114/