Android memory leak

    xiaoxiao2021-03-26  5

    Android leak canarytool


    How to check memory

    adb shell dumpsysmeminfo -d (android anonymous shared memory) clean memory)


    PSS total >(private dirty + clean)

    (both pss total andprivate dirty clean contains Zygnote allocation)


    Android studiomemory analysis tool


    Common possibleleaks


    Weak referenceexample


    common memoryleaks


    ByteBuffer.allocate()vs. ByteBuffer.allocateDirect()

    nondirect bufferallocate by jvm, store in jvm heap, may not continuously, gc will remove theallocate


    direct bufferallocated by calling through to native, operating system-specific code,bypassing the standard JVM heap ,

    intented forchannels and native I/O routines, The memory-storage areas of direct buffersare not subject to garbage collection because they are outside the standard JVMheap.


    if native code readnondirect buffer, it will create a temp direct buffer and copy nondirect todirect, after use release


    But nondirect hasits optimization like cache the temp direct buffer for future use.




    Don't hold thereference to an object outside its lifecycle

    create view withactivity(this) is dangerous

    static drawable isdangerous

    non static innerclass is dangerous