<分区>
我的应用程序有很多迭代。到目前为止,我还没有遇到任何内存问题。但是从代码层面我可以怀疑有几个地方会导致内存泄漏和内存不足的问题。我正在考虑手动调用垃圾收集器。手动调用垃圾收集器是一种好习惯吗?
<分区>
我的应用程序有很多迭代。到目前为止,我还没有遇到任何内存问题。但是从代码层面我可以怀疑有几个地方会导致内存泄漏和内存不足的问题。我正在考虑手动调用垃圾收集器。手动调用垃圾收集器是一种好习惯吗?
最佳答案
您可以使用以下方式调用垃圾收集器:
System.gc();
但这并不意味着它会立即执行。 JVM 决定何时执行它。一般来说,如果 JVM 即将抛出 OutOfMemoryError
,调用 System.gc()
不会阻止它。更好地调查为什么会泄漏如此多的内存并在此过程中进行清理。
Java文档:
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
关于java - 我们可以显式调用垃圾收集器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15632734/