Garbage Collection in android (Done manually)

18,363

Solution 1

it isn't good programming habit to call forcefully garbage collector (System.gc()) at the end of each activity

Because it is useless,only DVM decide when it should be call although you called it...

Solution 2

System.gc(), which the VM sometimes ignores at whim, is mostly useful in two cases:

  1. you're gobbling up memory like there's no tomorrow (usually with bitmaps).
  2. you suspect a memory leak (such as accidentally holding onto an old Context), and want to put the VM memory in a quiescent state to see if the memory usage is creeping up, for debugging.

Under nominal circumstances, one should not use it.

Solution 3

I really think it depends on your situation.

Because the heap is generational, the GC may not get rid of certain large objects or bitmaps on its first pass, and its heuristics may not indicate that additional garbage collection is necessary, but there are definitely scenarios where the heuristic could be wrong, and we as the developers have knowledge of a pattern, or can predict usage that the GC cannot, and therefore calling system.gc() will benefit us.

I have seen this before in specific scenarios such as dealing with map tiling or other graphic intensive behaviors, where the native GC in Android (even on 3.0+ devices), doesn't get it right, resulting in Out of Memory errors. However, by adding a few GC calls, the Out of Memory errors are prevented, and the system continues to process albeit at a slower rate (due to garbage collection). In graphic intensive operations, this usually is that state desired (a little lag) over the application crashing because it cannot load additional resources into memory.

My only explanation for why this happens in certain scenarios appears to be timing. If user operations are slow, then the native Android GC seems to do great. However, if your user is scrolling fast, or zooming quickly, this is where I have seen the Android GC lag behind, and a few well thought out System.gc() have resulted in my applications not crashing.

Solution 4

calling System.gc(), doesn't do any harm. but you cant be sure that it will be of some use. because you ask the DVM to do garbage collection, but can't command it... Its dependent totally on DVM. It calls when memory is running out or may be at any time..

Solution 5

I tried putting System.gc() on the line before the lines where I created my bitmap in my Android app. The garbage collector freed up several megabytes in some cases and put and end to my OutOfMemoryError conditions. It did not interfere with the normal garbage collection one bit but it did make my app run faster.

Share:
18,363
Tofeeq Ahmad
Author by

Tofeeq Ahmad

Love to code in java.I am an android application programmer.I have created 10 application in android.Currently i have complete 2D games now going for 3D game and augmented reality

Updated on July 14, 2022

Comments

  • Tofeeq Ahmad
    Tofeeq Ahmad almost 2 years

    I have a strange doubts. I know garbage collector has its own limitation. and if allocation is bad then it can cause a problem for application to respond in unusual way.

    So my question is that is it good programming habit to call forcefully garbage collector (System.gc()) at the end of each activity?

    Update

    Every one is saying that calling system.gc() not beneficial at all.Then i am wondering why its present here.DVM will decide when to run garbage collector.Then what is need of that method?

    Update 2

    Thanks community to help me out. But honestly i got knowledge about Garbage collection real Beauvoir from this link Java Performance Optimization

  • Tofeeq Ahmad
    Tofeeq Ahmad over 12 years
    Thank you all for quick responce.If i called it after each activity then how it will work.
  • Last Warrior
    Last Warrior over 12 years
  • Tofeeq Ahmad
    Tofeeq Ahmad over 12 years
    yes thanx..but it will to whom, who wanted to read garbage collection in java.But i want to know why system.gc() exist here if everything decide by DVM
  • Last Warrior
    Last Warrior over 12 years
    a use of system.gc() method stackoverflow.com/questions/3117429/…
  • Tofeeq Ahmad
    Tofeeq Ahmad over 12 years
    Ok..i got some idea about existence of system.gc().But let it be open for future best answer.Then i will accept one of them as a answer.But upvote for your effort
  • Tofeeq Ahmad
    Tofeeq Ahmad over 11 years
    then why system.gc() present?
  • Dave Newton
    Dave Newton over 11 years
    @Sameer Ask the original Java engineers. I'd assume so you could provide hints to the system-but its behavior is implementation-dependent. You know you asked this a year ago, right?
  • Tofeeq Ahmad
    Tofeeq Ahmad over 11 years
    yes i asked the question a year ago..But still i am not sure about use of System.gc(). So i assume that it hint JVM to run garbage collector (as you said just now) but can not force to JVM
  • Stephen C
    Stephen C almost 2 years
    "calling System.gc(), doesn't do any harm."- Actually, it does do harm. 1) It wastes CPU time, and if you want take it that far ... electricity!! 2) It injects more GC pauses.
  • Stephen C
    Stephen C almost 2 years
    There should be a better way to free up an (off-heap) bitmap than calling System.gc(). Surely ... stackoverflow.com/questions/20715442