Sunday, September 18, 2016

Memory and Memory Management in Java/Android

There are three type of physical memory in android.

  1. RAM - Random Accessed Memory - When running an application on your phone, the application is loaded from your device memory into the RAM, just like on a computer, the main purpose for this is access speed is far greater in RAM. You can clear the apps that is in the RAM on your phone depending on your smartphone (Samsung for example hold home button down and click ram manager > clear RAM - this will clear what's in RAM however most background apps will automatically be loaded back into the RAM hence the free space decreasing straight away
  2. As you mentioned SD Card is a external memory card which you can switch and swap with a variety of different sizes if needed
  3. Device Memory is your phones built in memory, just like SD Card has memory however this one cannot be swapped or taken out of the phone. It will hold all of your phones files that make it work, just like a PC, as standard you cannot edit these files as they are read only, however through rooting your phone you may access these files and change if needed.

     To understand the memory concept and memory management you have to understand how JVM provide the memory to your code.Mainly Java code is stored in the Heap and Stack memory. The main difference between heap and stack is that stack memory is used to store local variables and function call while heap memory is used to store objects in Java. No matter, where the object is created in code e.g. as a member variable, local variable or class variable,  they are always created inside heap space in Java.

    All the local variables and method calling is in stack,  If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.

     If you are using Recursion, on which method calls itself, You can quickly fill up stack memory. Another difference between stack and heap is that size of stack memory is a lot lesser than the size of  heap memory in Java.

    Variables stored in stacks are only visible to the owner Thread while objects created in the heap are visible to all thread. In other words, stack memory is kind of private memory of Java Threads while heap memory is shared among all threads.

    What is a memory leak?

    Some objects have a limited lifetime. When their job is done, they are expected to be garbage collected. If a chain of references holds an object in memory after the end of its expected lifetime, this creates a memory leak. When these leaks accumulate, the app runs out of memory.
    For instance, after Activity.onDestroy() is called, the activity, its view hierarchy and their associated bitmaps should all be garbage collectable. If a thread running in the background holds a reference to the activity, then the corresponding memory cannot be reclaimed. This eventually leads to an OutOfMemoryError crash.

    How do I avoid memory leaks?
  4. Avoid passing Context objects further that your activity or fragment
  5. NEVER  make/store a Context or View in a static variable. This is the first sign of a memory leak.                       
  6. private static TextView textView; //DO NOT DO THIS // private static Context context;
  7. Always unregister listeners in your onPause()/ onDestroy() methods. This includes Android listeners, to things such as Location services or display manager services and your own custom listeners.
  8. Don’t store strong references to activities in your AsyncTasks or background threads. Your activity may get closed, but your AsyncTask will continue execution and hold onto that reference of your activity.
  9. Use Context-application (getApplicationContext()) instead of Context from an activity if you can.
  10. Try not to use non-static inner classes if you can avoid it. Storing reference to something like an Activity or View inside this can lead to memory leaks. Use WeakReference if you need to store reference to them.
  11. Never give image in src in xml for preview, use instead tools:src for preview. It will take image for preview only but android:src loads image in memory, so that create a memory blocker if heavy image is used.   

No comments:

Post a Comment