“System.gc()”的作用是什么?在 J2ME 中?

发布于 2024-12-07 15:12:47 字数 241 浏览 1 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥有 2024-12-14 15:12:47

调用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.

薄荷港 2024-12-14 15:12:47

运行垃圾收集器。

调用gc方法表明Java虚拟机消耗
努力回收未使用的对象以释放内存
它们目前占用可快速重复使用。当控制权返回时
从方法调用来看,Java虚拟机已经尽力了
从所有废弃的对象中回收空间。

调用 System.gc() 实际上等同于调用:

 Runtime.getRuntime().gc()

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

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

○愚か者の日 2024-12-14 15:12:47

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文