开发者

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

开发者 https://www.devze.com 2023-04-10 16:31 出处:网络
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/exce

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,


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.


Runs the garbage collector.

Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

The call System.gc() is effectively equivalent to the call:

 Runtime.getRuntime().gc()

-> http://download.oracle.com/javase/6/docs/api/java/lang/System.html#gc%28%29


System.gc() and Runtime.getRuntime().gc() are equivalent. They suggest a garbage collection, but there is no guarantee that this will actually happen.

So, don't rely on it, and in fact, it is very rare that you want to call this at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号