Android Memory Leak - Anonymous class

Ram Kumar

will the below code will cause memory leak? Assume the method is in Activity

        public void main(){
        final Object obj = new Object();
        Runnable run = new Runnable() {
            @Override
            public void run() {
                Thread.sleep(25000);//sleep
                obj.hashCode();//do something
            }
        };
          new Thread(run).start();
    }

Does this lead to leak? Since the thread refer the Object which is created outside it

Thanks Ram

Thiago Valle

Yes, threads don't follow the Activity lifecycle, so if you create a thread and the Runnable is an anonymous class it will have an implicit reference to the Activity because java will create an inner class.

If the Activity orientation changes or any other type of configuration changes Android will destroy the Activity and create a new one, however, it can't be deallocated by the garbage collector because threads are considered GC roots, the net effect is that you will have 2 activities in memory until your thread ends. If your thread ends before a configuration change you're ok, but this is not recommended practice since you usually can't guarantee that.

A solution would be to declare the runnable as an inner static class or an external class, and when a configuration change happens any reference to the activity that you have in the Thread/Runnable you pass the reference of the new activity.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Anonymous runnable and memory leak

From Dev

Inherited class memory leak

From Dev

Doesn't using an anonymous class to add an action listener cause a memory leak?

From Dev

Memory consequences for an anonymous class

From Dev

Does android View reference passed to outside class create memory leak?

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 Dev

Reference to final object in anonymous class constructor a leak?

From Dev

Memory leak in my ByteToMessageDecoder class

From Dev

Android inner classes memory leak and leak by context?

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

Avoid memory leak with WeakReference Android

From Dev

How to release memory in android to avoid memory leak

From Dev

Android: avoiding passing activity to singletons by storing inside the Application class = memory leak?

From Dev

weak to non class types to avoid memory leak

From Dev

Memory leak caused by alloc in Class Method?