androidheap-memoryhandlerleakcanary

new Handler().postDelayed(new Runnable() memory leak


Concerning heap problem and memory leaks,
I read the following article that stressed on creating static class for the handler part:
here

Now is the following code prone to heap memory leak or not ?

///////////////////Handler 1
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        if (x == 1) {
            ///////////////////Handler 2            
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(y == 1) {
                        ///////////////////Handler 3                        
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                            //Do work A                             
                            }
                        }, 1000);
                    }
                    else
                    {
                        // do work B
                    }
                }
            }, 1000);
        }
        else
        {
            // do work B
        }
    }
}, 1000);

Solution

  • I made a proof test and the result is there is no memory leak detected by LeakCanary for the above code (to make sure LeakCanary is working true, I utilized another leaked sample of Handle issue and there was a Leak detected for that Handle)

    I'm not able to describe what is the exact difference yet