Search code examples
javamemory-managementjava-meout-of-memory

What is effect of "System.gc()" in J2ME?


I'm developing a mobile application in J2ME. Here I'm facing memory problem. I'm facing out of memory error. So please give the ideas of how it get rid out of this kind of error/exception, garbage collection, memory management in J2ME.

I had one doubt what is the effect System.gc() in the J2ME. What is the difference between System.gc() and Runtime.getRuntime().gc() in J2ME/Java.

Thanks & Regards,


Solution

  • Calling System.gc() will not fix an "OutOfMemoryError". An OOME only happens after the system has made a "best effort" attempt to release memory by garbage collecting (and other means) ... and failed to free enough memory to continue.

    The way to fix OOME errors is to find out what is using all of the memory and try to do something about it.

    Possible problems that can lead to OOMEs include:

    • Memory leaks; i.e. something in your app is causing lots of objects to remain "reachable" after they are no longer required.

    • Memory hungry data structures or algorithms.

    • Not enough memory to run the app with that input data.

    Your first step to solving this problem should be to use a profiler to see if there are any significant leaks, and to find out more generally what data structures are using all of the memory.