Leak leak using Context

I am doing some research, and I'm still not 100% sure if this could lead to a memory leak. I use the button view (v.context). I think I'm fine, as the context is not stored as Static, but I would like to get some feedback if possible. The main problem I see is with OSMonitor ... the value (M) goes up and up and up. With each opening / closing widget and screen rotation.

32M 43M 61M 77M etc ...

I'm not sure if (M) is megabytes or megabytes. If it is stack based, I assume pergege Megebits as most top-level devices are limited to 32/48 MB on the stack (or something else).

Thanks for the feedback / extra eyes!

This is a Banner app on the market, btw ...

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 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; } 
+4
source share
1 answer

There should be no leakage. Due to the nature of the Dalvik VM, the heap continues to grow when it is used until it reaches the maximum heap size. However, there may be enough space on the heap for your objects. I would suggest limiting the process memory (heap) in the emulator image and see if you really get an OutOfMemoryError. When creating an emulator, there is the property "Maximum VM VM heap size" that you want to set, for example. up to 32 (measured in megabytes).

If you get an OutOfMemoryError, you should take a closer look at Mcl Eclipse.

PS: I just realized that you should probably use the application context in your case and never execute an Activity. If you activate it from an activity, consider getApplicationContext instead of passing the Activity as a context. Static things can survive instances of Activity.

+2
source

Source: https://habr.com/ru/post/1385186/


All Articles