Tuesday, 6 December 2016

how to check memory leak





The Memory Monitor in Android Studio shows you how your app allocates memory over the course of a single session. The tool shows a graph of available and allocated Java memory over time, including garbage collection events. You can also initiate garbage collection events and take a snapshot of the Java heap while your app runs. The output from the Memory Monitor tool can help you identify points when your app experiences excessive garbage collection events, leading to app slowness.
For more information about how to use Memory Monitor tool, see Viewing Heap Updates.

http://www.vogella.com/tutorials/EclipseMemoryAnalyzer/article.html


Tracking memory allocations can give you a better understanding of where your memory-hogging objects are allocated. You can use Allocation Tracker to look at specific memory uses and to analyze critical code paths in an app such as scrolling.

Although it is not necessary or even possible to remove all memory allocations from your performance-critical code paths, Allocation Tracker can help you identify important issues in your code. For example, an app might create a new Paint object on every draw. Making the Paint object global is a simple fix that helps improve performance.
  1. Start your app on a connected device or emulator.
  2. In Android Studio, select View > Tool Windows > Android Monitor.
  3. In the upper-left corner of Android Monitor, select the Monitors tab.
  4. In the Memory Monitor tool bar, click Allocation Tracker  to start memory allocations.
  5. Interact with your app.
  6. Click Allocation Tracker  again to stop allocation tracking.
    Android Studio creates an allocation file with the filename application-id_yyyy.mm.dd_hh.mm.alloc, opens the file in Android Studio, and adds the file to the Allocations list in the Captures tab.
  7. In the allocations file, identify which actions in your app are likely causing too much allocation and determine where in your app you should try to reduce allocations and release resources


Relasae memory on onstop()



When your activity receives a call to the onStop() method, it's no longer visible and should release almost all resources that aren't needed while the user is not using it. Once your activity is stopped, the system might destroy the instance if it needs to recover system memory. In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory.

No comments:

Post a Comment