我们可以明确地调用垃圾收集器吗? [英] Can we call the Garbage Collector explicitly?

查看:80
本文介绍了我们可以明确地调用垃圾收集器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有很多迭代。直到现在我还没有遇到任何内存问题。但从代码级我可以怀疑有几个地方,这会导致内存泄漏和内存不足的问题。我正在考虑手动调用垃圾收集器。手动调用垃圾收集器是否是一个好习惯?

解决方案

您可以使用以下方式调用垃圾收集器:

  System.gc(); 

但这并不意味着它会立即执行。 JVM决定何时执行它。通常,如果JVM将要抛出 OutOfMemoryError ,调用 System.gc()不会阻止它。更好地调查为什么你会漏掉这么多的记忆并一路清理它。

JavaDoc


调用gc方法建议Java虚拟机花费
努力回收未使用的对象,以便使它们当前占用的内存
可供快速重用。当控制从方法调用返回
时,Java虚拟机已尽最大努力
从所有被丢弃的对象中回收空间



My application has lot of iterations. Till now I haven't faced any memory issues. But from code level I can suspect there are few places, which causes memory leaks and out of memory problem. I am thinking of calling garbage collector manually. Is it good practice to call Garbage Collector manually?

解决方案

You can call Garbage collector using:

System.gc();

But this does not mean that it'll be executed immediately. The JVM decides when to execute it. In general if the JVM is about to throw an OutOfMemoryError, calling System.gc() won't prevent it. Better investigate why you're leaking so much memory and clean it up along the way.

JavaDoc:

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

这篇关于我们可以明确地调用垃圾收集器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆