Download presentation
Presentation is loading. Please wait.
Published byanudeep bhukya Modified over 6 years ago
1
Memory leaks in Android
2
Introduction In our day-to da life many apps crash,and the reason one will point to it is memory leak In majority of the cases we see a kind of unpredictable increase in memory usage of app from the time it got started in the foreground and cannot allocate more resources and subsequently leading to a crash. In Java this often results in an OutOfMemoryException being thrown
3
A memory leak reduces the performance of the android by reducing the amount of available memory. In some of the cases, too much of the available memory may become allocated and all or part of the system or device stops working correctly, the application fails, or the system slows down vastly due to thrashing.
4
How to fix Memory leak First Step: In the way of fixing a memory leak, we first have to make a deep diagnosis around it.And fortunately we have the Android Studio,which supports a built in tool called Android Monitor and we can use to observe memory usage among other things. All to do is open the Android Monitor and go to the Monitors tab to see how much memory is used and allocated in real time. Any interactions causing resource allocation will be shown here, memory at that particular time,how is engaged will be shown.
5
The following three things can be known See how your app allocates memory over over time. The Memory Profiler shows a realtime graph of how much memory your app is using, the number allocated Java objects, and when garbage collection occurs. Initiate garbage collection events and take a snapshot of the Java heap while your app runs. Record your app's memory allocations and then inspect all allocated objects, view the stack trace for each allocation, and jump to the corresponding code in the Android Studio editor.
6
Second Step To avoid running out of memory, you can make the queries to the system to determine how much heap space you have available on the current device. You can query the system for this figure by calling getMemoryInfo(). This returns an ActivityManager.MemoryInfo object that provides information about the device's current memory status, including available memory, total memory, and the memory threshold—the memory level at which the system begins to kill processes. The ActivityManager.MemoryInfo object also exposes a simple boolean, lowMemory that tells you whether the device is running low on memory
7
Third step Neglected a service running when it’s not needed is one of the worst careless memory-management mistakes an Android app can undergo. If your app needs a service to perform work in the background, do not keep it running unless it needs to run a job. Remember to stop your service when it has completed its task. Otherwise, you can inadvertently cause a memory leak. When you start a service, the system prefers to always keep the process for that service running. This behavior makes services processes very expensive because the RAM used by a service remains unavailable to other processes. This reduces the number of cached processes that the system can keep in the LRU cache, making app switching less efficient.
8
You should generally avoid use of persistent services because of the on- going demands they place on available memory. Instead, we recommend that you use an alternative implementation such as JobScheduler. For more information about how to use JobScheduler to schedule background processes, see Background Optimizations.
9
For more information please visit android training in bangaloreandroid training in bangalore
10
THANKYOU
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.