Android memory leak on static Resource member variable?

Bartosz Bilicki

Is it safe to have static reference to private static Resources mRes; in my Utils class, initalized as follows?

public static void init(Resources res) {
    mRes = res;
}
.. later in activity
Utils.init(getContext().getResources());

It seems to me it causes memory leak (log from Eclipse Memory Analyzer below)

mOuterContext android.app.ContextImpl 
'- mContext android.content.res.Resources
  |- mRes class com.github.mikephil.charting.utils.Utils
  |- mResources android.app.LoadedApk
  |- mResources android.app.ContextImpl
  |- this$0 android.content.res.Resources$Theme
  |  '- referent java.lang.ref.FinalizerReference
  |     '- next java.lang.ref.FinalizerReference
  |        '- next java.lang.ref.FinalizerReference

Are there safe ways to get reference to Resources class, that is not leaking whole activity?

ToYonos

Here is a solution, use a static reference of your Application Context, held by your Application

public class MyApplication extends Application
{
    private static Context context;

    public static Resources getResourcesStatic()
    {
         return context.getResources();
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        this.context = this.getApplicationContext();
    }
}

Now just call MyApplication.getResourcesStatic() to access your resources wherever you are.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android context memory leak via activity private member

From Dev

Android animations memory leak

From Dev

Is it a memory leak in android

From Dev

memory leak with Android WebView

From Dev

Is it a memory leak in android

From Dev

Android - is this a memory leak?

From Dev

Prevent memory leak in Android

From Java

Pointer to member variable as static member

From Dev

Global variable and realloc and memory leak

From Dev

In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

From Dev

Android inner classes memory leak and leak by context?

From Dev

Jersey Client, memory leak, static and concurrency

From Dev

SpriteKit Memory Leak on static Menu scene

From Dev

Jersey Client, memory leak, static and concurrency

From Dev

SpriteKit Memory Leak on static Menu scene

From Java

Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

From Dev

Why does Android leak memory due to static Drawable if it's callback is reset?

From Dev

"Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

From Dev

Android - Device Memory Leak with Fragments

From Dev

Android Fragment Webview Memory Leak

From Dev

Android runOnUiThread causing memory leak

From Dev

Android camera Bitmap memory leak

From Dev

Android progress bar memory leak

From Dev

`Unknown` (`Other`) memory leak in Android?

From Dev

Android runOnUiThread causing memory leak

From Dev

Getting Memory leak on android fragment

From Dev

Android memory leak Custom view

From Dev

Android memory leak on device, not on emulator

From Dev

Android Memory Leak - Anonymous class

Related Related

  1. 1

    Android context memory leak via activity private member

  2. 2

    Android animations memory leak

  3. 3

    Is it a memory leak in android

  4. 4

    memory leak with Android WebView

  5. 5

    Is it a memory leak in android

  6. 6

    Android - is this a memory leak?

  7. 7

    Prevent memory leak in Android

  8. 8

    Pointer to member variable as static member

  9. 9

    Global variable and realloc and memory leak

  10. 10

    In Java can making a local variable final in a non-static method that is called many times cause a memory leak?

  11. 11

    Android inner classes memory leak and leak by context?

  12. 12

    Jersey Client, memory leak, static and concurrency

  13. 13

    SpriteKit Memory Leak on static Menu scene

  14. 14

    Jersey Client, memory leak, static and concurrency

  15. 15

    SpriteKit Memory Leak on static Menu scene

  16. 16

    Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)

  17. 17

    Why does Android leak memory due to static Drawable if it's callback is reset?

  18. 18

    "Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

  19. 19

    Android - Device Memory Leak with Fragments

  20. 20

    Android Fragment Webview Memory Leak

  21. 21

    Android runOnUiThread causing memory leak

  22. 22

    Android camera Bitmap memory leak

  23. 23

    Android progress bar memory leak

  24. 24

    `Unknown` (`Other`) memory leak in Android?

  25. 25

    Android runOnUiThread causing memory leak

  26. 26

    Getting Memory leak on android fragment

  27. 27

    Android memory leak Custom view

  28. 28

    Android memory leak on device, not on emulator

  29. 29

    Android Memory Leak - Anonymous class

HotTag

Archive