简体   繁体   English

在java中调用System.gc()是否建议终生代和年轻代的垃圾收集?

[英]Does invoking System.gc() in java suggest garbage collection of the tenured generation as well as the young generation?

When invoking System.gc() in java (via JMX), it will dutifully (attempt to) clean the young generation. 在java(通过JMX)中调用System.gc()时,它将尽职尽责地(尝试)清理年轻一代。 This generally works very well. 这通常很有效。 I have never seen it attempt to clean the tenured generation, though. 但我从未见过它试图清理终身一代。 This leads me to two questions: 这引出了两个问题:

  1. Can the tenured generation even be collected (ie is there actually garbage in this generation, or do all objects in the tenured generation actually still have live references to them)? 可以在年老代,甚至采集(即有没有真正的垃圾在我们这一代,还是在年老代中的所有对象其实还是有活对它们的引用)?
  2. If the tenured generation can be collected, can this be done via System.gc(), or is there another way to do it (unlikely), or will I simply have to wait until I run out of space in the tenured generation? 如果可以收集终身代,可以通过System.gc()来完成,还是有另一种方法(不太可能),或者我只需要等到终身用完了?

http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html#other_considerations states that System.gc() triggers a major collection, ie including tenured. http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html#other_considerations表明System.gc()触发了一个major集合,即包括终身集合。

Have you seen this not to be the case in the GC logs? 您是否在GC日志中看到过这种情况?

关于Java垃圾收集的特别文章: 使用Java 5 VM调优垃圾收集它专门针对Java 5,但大多数可能仍适用于以后的VM。

I beg to differ. 我不敢苟同。 The Java 6 GC tuning guide actually says this: Java 6 GC调优指南实际上是这样说的:

This can force a major collection to be done when it may not be necessary (ie, when a minor collection would suffice), and so in general should be avoided. 可以强制在可能没有必要时进行主要收集(即,当次要收集就足够时),因此通常应该避免。

Note the use of the word "can" rather than "will". 注意使用“can”而不是“will”。 My reading of this sentence is that it does not state that a major collection will be done. 我对这句话的解读是,它没有说明将要完成一个主要的收藏。 It might be done, or it might not. 它可能会完成,也可能不会。 The point that I think the authors are really trying to make here (and elsewhere) is that calling System.gc() may cause the collector to do a lot of unnecessary work. 我认为作者真正想在这里(以及其他地方)做的一点是调用System.gc()可能会导致收集器做很多不必要的工作。

Now it may be that calling System.gc() does cause a major collection each time ... for a certain versions of the JVM. 现在可能是调用System.gc() 确实每次都会导致一个主要的集合...对于某个版本的JVM。 But you should not rely on this being the case for all versions, especially future ones. 但是你不应该依赖所有版本的情况,特别是未来的版本。

Simple thumb rule for garbage collection in java is, java中垃圾收集的简单拇指规则是,

  • Use memory cleanup techniques like cleaning up resources, members and instances when not needed. 使用内存清理技术,例如在不需要时清理资源,成员和实例。
  • Close what you open. 关闭你打开的东西。 (WRT connection, hibernate session etc...) (WRT连接,休眠会话等......)
  • Use buffering techniques while using file IO. 使用文件IO时使用缓冲技术。
  • Use new NIO instead of older File IO. 使用新的NIO而不是旧的File IO。
  • Use Collections class from java utils for collection manipulations. 使用java utils中的Collections类进行集合操作。
  • Use Array when possible. 尽可能使用Array。
  • Learn techniques specific to frameworks. 学习特定于框架的技术。 Say in hibernate, do not use ArrayList for one-to-many relationships, because Lists are ordered so it will make extra column for children's ordering. 在休眠中说,不要将ArrayList用于一对多关系,因为Lists是有序的,因此它会为子项的排序添加额外的列。 Use Set instead. 请改用Set

  • Do not use Hsql. 不要使用Hsql。 Use some sort of relational database like postgres etc... HSQL will eat more memory. 使用某种关系数据库,如postgres等...... HSQL会占用更多内存。 I have faced issues regarding this. 我遇到过有关这方面的问题。

  • Also keep in mind that when you using XML handling, do not use DOM when you just want to read small amount of data form XML. 还要记住,当您使用XML处理时,如果您只想从XML读取少量数据,请不要使用DOM。 DOM will make whole structure of XML in memory, so will take more memory. DOM将在内存中构建XML的整体结构,因此将占用更多内存。

  • Try not to keep objects in memory which will grow with time. 尽量不要将对象保留在内存中随时间增长。 Otherwise the applications can be out of memory. 否则应用程序可能内存不足。

  • define sizes for your lists, maps etc... 定义列表,地图等的大小......

  • Just do not use any framework for small needs. 只是不要使用任何框架来满足小需求。 If so then carefully check how you can tweak configurations for better and smaller heap area. 如果是这样,那么仔细检查如何调整配置以获得更好和更小的堆区域。

Just calling System.gc() doen't free your memory and cleaning up objects. 只是调用System.gc()不要释放你的记忆和清理对象。

Guys, please add more details on this if i missed. 伙计们,如果我错过了,请添加更多细节。 So Others can check it out for better performance. 所以其他人可以检查一下以获得更好的性能。 thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM