“System.gc()”的作用是什么?在 J2ME 中?
我正在 J2ME 中开发移动应用程序。我在这里面临内存问题。我面临内存不足错误。所以请给出J2ME中如何摆脱这种错误/异常、垃圾收集、内存管理的想法。
我有一个疑问System.gc()
在J2ME中的作用是什么。 J2ME/Java 中的 System.gc()
和 Runtime.getRuntime().gc()
之间有什么区别。
谢谢&问候,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用
System.gc()
不会修复“OutOfMemoryError”。 OOME 仅在系统“尽最大努力”尝试通过垃圾收集(和其他方式)释放内存后发生……但未能释放足够的内存以继续。修复 OOME 错误的方法是找出是什么占用了所有内存,并尝试采取措施。
可能导致 OOME 的问题包括:
内存泄漏;即您的应用程序中的某些内容导致许多对象在不再需要后仍保持“可访问”。
内存消耗大的数据结构或算法。
内存不足,无法使用该输入数据运行应用。
解决此问题的第一步应该是使用探查器来查看是否存在任何重大泄漏,并更普遍地找出哪些数据结构正在使用所有内存。
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.
-> http://download.oracle .com/javase/6/docs/api/java/lang/System.html#gc%28%29
-> http://download.oracle.com/javase/6/docs/api/java/lang/System.html#gc%28%29
System.gc() 和 Runtime.getRuntime().gc() 是等效的。他们建议进行垃圾收集,但不能保证这实际上会发生。
因此,不要依赖它,事实上,您很少想调用它。
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.