i am doing a game programming in J2ME, I have alot of memory issue to hadle and i heard of the System.gc() method to call the garbage collector to collect the garbage. And i heard from others that when u call the Collector it does not stright away comes in and collect the garbage. So i would like to find out which is the most effective way to do garbage collection in the mobile game application. i am open to any suggestion. thanks in advance
There is no way to force garbage collection. Calling System.gc() is only a *suggestion* and the JVM is likely to ignore it. Even using Thread.sleep() will not guarantee that the garbage collector runs.
Alternatively, you can use Runtime.getRuntime().gc() - But this does not guarantee GC either. You are basically left with no option but to call either of these methods and cross your fingers hoping the GC will run.
But, before the JVM crashes throwing OutOfMemoryError, the GC would surely be run once - A Silver lining.