I am creating an activity in which there is an api hit.
But when i am opening that activity 3-4 times ,it starts crashing and shows this :
Background sticky concurrent mark sweep GC freed 46562(2MB) AllocSpace objects, 0(0B) LOS objects, 15% free, 13MB/15MB, paused 7.087ms total 41.571ms
Skipped 241 frames! The application may be doing too much work on its main thread.
Skipped 356 frames! The application may be doing too much work on its main thread,
In android profiler I found out that it does not free memory on finishing the activity and use more memory on open the activity again which lead to high memory usage and ultimately leads to crash.
I have tried everything searched google,stackover flow,android hive but nothing worked.
I think you should always make asynchronous call in a background thread, example call API. Handle too much task on main thread cause your performance hiccups due to garbage collection GC freed 46562. Make sure you don't keep context or anything can cause leak memory when you hit API. Maybe I can help you better if you show your code.
Related
I'm getting this log in my logcat
I/zygote: Background concurrent copying GC freed 148531(5MB) AllocSpace objects, 18(936KB) LOS objects, 32% free, 12MB/18MB, paused 3.758ms total 217.199ms
is this something that needs to be addressed or it is can be ignored
This is not an error, it's just an Android log message notifying you about when garbage collection takes place. Everything's normal. The log messages don't harm your app. It's only a problem if you go out of memory, or you see performance hiccups due to garbage collection.
If you are seeing this frequently (or consistently), then you are likely allocating too many objects. A common cause is allocating many (or a few large) objects within a loop
Every time we hit this loop, we allocate one hundred new Bitmap objects.
The best way to prevent GC sweeps is to not allocate objects. Of course you have to allocate objects in Java, so you need to ensure that you are not allocating unnecessarily.
Finally, you can always filter or ignore the logs. :D
This page may help you with further research: https://blog.gceasy.io/2017/05/09/understanding-android-gc-logs/
I would like to know that why does the below message shows up everytime while running any application.
12-11 17:18:37.141: D/dalvikvm(3155): GC_CONCURRENT freed 485K, 9% free 6696K/7303K, paused 9ms+335ms
paused 9ms+335ms due to this pause my audio which i supposed to play is missing, because as per my code it receives audio data every 40ms so here it paused for 9ms+335ms which is 10 times data loss
I know its performing some kind of Garbage Collection but my question is why does it frequently comes in the logcat.
Thanks for any kind of help!!!!!!
Garbage Collection is used by Java to prevent you from going Out of memory by removing Objects that are no longer referenced by any class and cannot be reached from your code.
If you have too many of these objects, you will get GC calls alot, which may at some times affect your performance. On the other hand, having Objects that are referenced all the time, may prevent GC from being called (memory leak) and your memory may fill up and you get an OutOfMemoryException.
Your goal is not to eliminate GC, but reduce them as much as possible in methods that are delay sensitive and are running on the UI thread (like onDraw(), getView(), ... for e.g.).
That is normal behaviour for Android. The phone just does garbage collection.
For more information checkout the great Google I/O video on this:
Google I/O 2011: Memory management for Android Apps
Folks, check out these doc/links,its related to memory management:
http://static.googleusercontent.com/media/www.google.com/en//events/io/2011/static/presofiles/memory_management_for_android_apps.pdf
http://eclipse.org/mat/
http://therockncoder.blogspot.in/2012/09/fixing-android-memory-leak.html
I am instrumenting the Dalvik VM and would like to know if there are any tools to analyze garbage collection in dalvik. I know about allocation tracker but I'm looking for something more elaborate.
Get a log of all GC operations over time:
Every time a GC takes place, you get a line in your LogCat.
08-08 16:42:21.998: D/dalvikvm(26942): GC_CONCURRENT freed 773K, 26% free 4739K/6368K, paused 4ms+3ms, total 92ms
08-08 16:42:21.998: D/dalvikvm(26942): WAIT_FOR_CONCURRENT_GC blocked 11ms
It seems I'm getting those for all apps on my device.
This line includes lots of interesting stats about the GC like the amount of memory freed, how long the GC took, when exactly did it happen and the size of your heap (used/total).
Since all of those log lines have the tag dalvikvm, you should be able to collect and filter them over a long period of time and analyze to learn about GC behavior.
Analyzing a specific run of the GC:
If you want to analyze what happens in one specific GC operation, the best tool for the job is Eclipse MAT. Eclipse MAT can parse heap dumps. Take a heap snapshot, wait for the GC (or trigger it yourself using DDMS), and then take another snapshot.
Eclipse MAT can show you the delta between the two snapshots. Notice you'll see both new allocations and GC-caused deallocations. More info about comparing snapshots is available here.
Some other thoughts:
I'm not sure how much you would be able to learn from analyzing the GC process. The inner workings of the GC are an implementation detail. It can change without notice between OS versions / devices / configurations.
I'm trying to think of ways to improve the GC latency you're experiencing.. It seems to me the GC usually runs when memory conditions are low. This probably happens during new allocations, therefore the GC might be running while your service is active. Maybe, if you use the time your service is inactive to GC manually, you would be able to reduce the number of GC's happening in the critical path of responding to a web request. To try that, I would add a simple background timer, and reset it whenever my service becomes active (new request). When the timer ticks (inactivity for some period of time), I would run System.gc() manually.
If you see the Android logs, you may see a lot of those things.
What do they mean, knowing those may help us doing better memory allocations.
Example:
28470 dalvikvm D GC_FOR_MALLOC freed 665 objects / 239992 bytes in 71ms
28470 dalvikvm D GC_FOR_MALLOC freed 673 objects / 240288 bytes in 87ms
21940 dalvikvm D GC_EXPLICIT freed 4802 objects / 185320 bytes in 78ms
28470 dalvikvm D GC_FOR_MALLOC freed 666 objects / 240536 bytes in 63ms
GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to perform an allocation. Might be triggered when new objects are being created.
GC_EXPLICIT means that the garbage collector has been explicitly asked to collect, instead of being triggered by high water marks in the heap. Happens all over the place, but most likely when a thread is being killed or when a binder communication is taken down.
There are a few others as well:
GC_CONCURRENT Triggered when the heap has reached a certain amount of objects to collect.
GC_EXTERNAL_ALLOC means that the the VM is trying to reduce the amount of memory used for collectable objects, to make room for more non-collectable.
Update: There has been a name-change of the first event in later versions of Android. It's now called "GC_FOR_ALLOC".
There is also a new event available, although very rare in modern phones:
GC_BEFORE_OOM means that the system is running really low on memory, and that there is a final GC performed, in order to avoid calling the low memory killer.
Another place where the Dalvik garbage collector messages are explained is in this video: Google I/O 2011: Memory management for Android Apps
At about 14 minutes into the presentation, he breaks down the message format. (BTW, that video has really good info on debugging memory leaks)
Roughly speaking, the format is [Reason] [Amount Freed], [Heap Statistics], [External Memory Statistics], [Pause Time]
Reason
Robert/yuku already gave info on the meaning of these.
Amount Freed
E.g. freed 2125K
Self explanatory
Heap Statistics
E.g. 47% free 6214K/11719K
These numbers reflect conditions after the GC ran. The "47% free" and 6214K reflect the current heap usage. The 11719K represents the total heap size. From what I can tell, the heap can grow/shrink, so you will not necessarily have an OutOfMemoryError if you hit this limit.
External Memory Statistics
E.g external 7142K/8400K
Note: This might only exist in pre-Honeycomb versions of Android (pre 3.0).
Before Honeycomb, bitmaps are allocated external to your VM (e.g. Bitmap.createBitmap() allocates the bitmap externally and only allocates a few dozen bytes on your local heap). Other examples of external allocations are for java.nio.ByteBuffers.
Pause Time
If it's a concurrent GC event, there will be two times listed. One is for a pause before the GC, one is for a pause when the GC is mostly done.
E.g. paused 3ms+5ms
For non-concurrent GC events, there is only one pause time and it's typically much bigger.
E.g. paused 87ms
I also found this in the Android sources, dalvik/vm/alloc/Heap.h. May this be useful.
typedef enum {
/* Not enough space for an "ordinary" Object to be allocated. */
GC_FOR_MALLOC,
/* Automatic GC triggered by exceeding a heap occupancy threshold. */
GC_CONCURRENT,
/* Explicit GC via Runtime.gc(), VMRuntime.gc(), or SIGUSR1. */
GC_EXPLICIT,
/* GC to try to reduce heap footprint to allow more non-GC'ed memory. */
GC_EXTERNAL_ALLOC,
/* GC to dump heap contents to a file, only used under WITH_HPROF */
GC_HPROF_DUMP_HEAP
} GcReason;
While running my apps, I have got this kind of logs:
GC_EXTERNAL_ALLOC freed 2K, 38% free 8772K/14087K, external 17480K/17998K, paused 87ms
GC_FOR_MALLOC freed 0K, 38% free 8772K/14087K, external 17480K/17998K, paused 67ms
GC_CONCURRENT freed 2125K, 47% free 6214K/11719K, external 7142K/8400K, paused 3ms+5ms
Does anyone know what these logs mean? Thanks in advance!
What is the difference between 'GC_EXTERNAL_ALLOC', 'GC_FOR_MALLOC' and 'GC_CONCURRENT'. Are there some other 'GC' events?
What does '38% free 8772K/14087K' mean? What is '8772K' and what is '14087K'?
What does 'external 17480K/17998K' mean? What is '17480K' and what is '17998K'?
Thanks!
Another place where the Dalvik garbage collector messages are explained is in this video: Google I/O 2011: Memory management for Android Apps
At about 14 minutes into the presentation, he breaks down the message format. (BTW, that video has really good info on debugging memory leaks)
Roughly speaking, the format is [Reason] [Amount Freed], [Heap Statistics], [External Memory Statistics], [Pause Time]
Reason
Viktor/Robert already explained GC_CONCURRENT, GC_FOR_MALLOC, GC_EXTERNAL_ALLOC.
There is also:
GC_HPROF_DUMP_HEAP - If you dump heap by clicking the "dump heap" button from DDMS or programatically
GC_EXPLICIT - If you call System.gc()
Amount Freed
E.g. freed 2125K
Self explanatory
Heap Statistics
E.g. 47% free 6214K/11719K
These numbers reflect conditions after the GC ran. The "47% free" and 6214K reflect the current heap usage. The 11719K represents the total heap size. From what I can tell, the heap can grow/shrink, so you will not necessarily have an OutOfMemoryError if you hit this limit.
External Memory Statistics
E.g external 7142K/8400K
Note: This might only exist in pre-Honeycomb versions of Android (pre 3.0).
Before Honeycomb, bitmaps are allocated external to your VM (e.g. Bitmap.createBitmap() allocates the bitmap externally and only allocates a few dozen bytes on your local heap). Other examples of external allocations are for java.nio.ByteBuffers.
Pause Time
If it's a concurrent GC event, there will be two times listed. One is for a pause before the GC, one is for a pause when the GC is mostly done.
E.g. paused 3ms+5ms
For non-concurrent GC events, there is only one pause time and it's typically much bigger.
E.g. paused 87ms
I was also looking for this information.
GC stands for garbage-collector, which collects unused objects during runtime of your app.
GC_EXTERNAL_ALLOC: Means that the VM is trying to reduce the amount of memory used collectable objects, to make room for non-collectable objects.
GC_FOR_MALLOC: Means that the GC was triggered because there wasn't enough memory left on the heap to perform an allocation. Might be triggered when new objects are being created.
GC_CONCURRENT: Triggered when the heap has reached a certain amount of objects to collect.
I am guessing that the 38% free means that 38% of the heap is unused. 8772K would be the size of the used memory on the heap and 14087K would be the size of the heap.
I donĀ“t know, sorry.
Please note that all of this information is based on a post from Robert on a similiar question posted here on stackoverflow. All credit (as long as this is correct) goes to Robert.