Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I found that you can control your memory usage on libgdx with:

int javaHeap =;
int nativeHeap =;

But, How can I force a garbage collector cleanup? I want to force in special moments, like screen change on a game.

share|improve this question
My issues is same as you, @Zhen, but not on getJavaHeap nor getNativeHeap(). When I'm performing several screen changes, I tested on the desktop and it worked countlessly but not in device, which ends up at 6th or 7th screen. – David Dimalanta Jan 29 '13 at 3:38
up vote 5 down vote accepted

Java's garbage collector is nondeterministic. There's System.gc(); but it will likely do nothing. See the StackOverflow question for further discussion.

share|improve this answer
I have a question, @Anko. Is it a bad practice to call or declare a forced garbage collector according to your answer? – David Dimalanta Jan 29 '13 at 3:42
@David There's a SO question about that too. A summary of the answers: It is bad practice. System.gc(); is not defined to do any particular thing. Depending on many unknown factors, it may do one of many even potentially harmful things. – Anko Jan 29 '13 at 10:24
Then that means I have to make the program nice and simple, right? – David Dimalanta Jan 30 '13 at 2:24
@David You always should! :) – Anko Jan 30 '13 at 9:30
There is another question regarding your answer, @Anko. Is it won't matter even if I manage the memory using a simple Java's getters and setters, virtual memory will still depleting or there is an exception when using a static keyword? – David Dimalanta May 9 '13 at 8:04

Many of the libgdx classes have memory allocated across JNI. Destroying/disposing of classes should handle this for you. Forcing a GC call may not be de-allocating classes if they reside in native managed memory. Last time I used libgdx, it provided decent logs of when memory was allocated/destroyed. Do yo have a specific issue when loading new screens?

Update: Based on this page, there's a screen.dispose() and comments suggesting that you should reuse your screen class to avoid GC.

share|improve this answer

While you can give a hint to the runtime engine via System.gc() this is a "hint" and not a command so you cannot rely on it to actually trigger garbage collection. Even if it were a command, forcing garbage collection is unlikely to accomplish what you want- the garbage collection process itself consumes resources so when it runs your game will be impacted. So, in fact, you want to avoid garbage collection to ensure your game isn't contending with the GC process for resources, see this link for a libgdx specific discussion:

Instead of dynamically allocating many short lived objects that require garbage collection I think the better strategy would be to allocate long life objects at times the user isn't playing, e.g. loading screen or first start up. These objects can even be returned to a pool when no longer needed and then reused as necessary.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.