何时垃圾收集 [英] When to Garbage Collect

查看:316
本文介绍了何时垃圾收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码在内存中加载一个非常大的图像。因此,称之为

  System.gc(); 

加载图片之前。我可以告诉它没有问题。



昨天我决定使用一个非常有用的软件,叫做 FindBugs 可以扫描您的代码并报告可能导致错误或通常不会被建议的策略的问题。问题是我提到的这段代码被报道。描述是这样的:


...强制垃圾收集;
非常令人怀疑,除了
基准代码外

接下来详细阐述:


代码显式调用垃圾
集合。除了在
基准测试中的具体用途,这是非常可疑的。

在过去,人们
明确调用垃圾
收藏家在例如close或
finalize方法的例程中导致了巨额的
性能黑洞。垃圾
集合可能很昂贵。任何
的情况,迫使数百或
数千垃圾收集将
带来机器抓取。


所以我的问题是:在这种情况下以编程方式调用垃圾收集器是不行的?我的代码只调用一次,它所使用的方法很少被使用。如果不能调用它,那么在进行内存密集型操作之前需要尽可能多的内存,并且您需要在释放内存之前释放尽可能多的内存,您应该怎么做?

解决方案

您是否通过System.gc()获得了任何性能改进?
我不这么认为,因为在加载图像之前你可能没有很多需要收集的对象。



通常现代的垃圾收集器知道什么时候跑,所以你不应该强制收集,除非你有一个真正的好理由。 (例如该插件建议的基准测试应用程序)

btw:调用System.gc()建议VM执行完整或大型集合,这意味着所有线程都会很快停止。
否则,它可能只会产生小垃圾回收,不会停止所有线程。



使用-verbose:gc运行您的程序,以查看收集了很多字节。

这里还有大量有关垃圾收集的技术信息:
http://java.sun.com/developer/technicalArticles/Programming/GCPortal/


I have a piece of code that load a very big image in memory. So it seemed like a reasonable thing to call

System.gc();

before loading the image. From what I can tell it works with no issues.

Yesterday i decided to use a pretty useful piece of software called FindBugs that scans your code and reports back issues that might cause bugs or generally not advised strategies. The problem is that this piece of code i mentioned gets reported. The description is this:

... forces garbage collection; extremely dubious except in benchmarking code

And it goes on to elaborate :

Code explicitly invokes garbage collection. Except for specific use in benchmarking, this is very dubious.

In the past, situations where people have explicitly invoked the garbage collector in routines such as close or finalize methods has led to huge performance black holes. Garbage collection can be expensive. Any situation that forces hundreds or thousands of garbage collections will bring the machine to a crawl.

So my question is : Is it NOT OK to programmatically call the garbage collector in such a case? My code only calls it once and the method that it is in gets used rarely. And if it is not OK to call it then what should you do in a case where you need as much memory as possible before doing a very memory intensive operation and you need to free as much memory as posible prior to it?

解决方案

Did you get any performance improvements with the System.gc()? I don't think so, since you probably dont have a lot of objects that needs to be collected before you load the image.

Usually modern garbage collectors know best when to run, so you shouldnt force a collection, unless you have a really really good reason to. (for example a benchmarking application as suggested by that plugin)

btw: Calling System.gc() recommends the VM to perform a "full" or "large" collection, which means that all threads are stopped shortly. Otherwise it will probably only make "small" garbage collections, which don't stop all threads.

Run your program with -verbose:gc to see how many bytes are collected.

There is also lots of technical information on garbage collection here: http://java.sun.com/developer/technicalArticles/Programming/GCPortal/

这篇关于何时垃圾收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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