java中垃圾回收的疑问 [英] Doubts on Garbage Collection in java

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

问题描述

我知道很多关于垃圾收集的问题,并且我已经通过了他们,但我仍然有一些疑问。


  1. 如果我们不能强制JVM进行垃圾收集,那么 System.gc()的需求是什么?在哪种情况下,它是有用的?


  2. 我知道年轻一代[伊甸园,SO,S1]和老一代,以及物件是如何从年轻一代转移到旧的代。什么时候将一个对象转移到永久代?例如,我有 mycar 对象具有引用并且不符合垃圾回收的条件,因此何时 mycar 对象将会移动永久代?

  3. 我已经读过,永久代中存储的静态变量。什么时候他们将被垃圾收集,以及哪些类型的其他物体存储在永久代中?为什么静态变量存储在永久生成中,永久生成的用途是什么? 我知道对象存储在堆内存中。这是真的,每个应用程序都有它自己的堆内存吗? $ c>降低了应用程序的性能并减慢了我们的应用程序的速度或者每当垃圾收集由JVM完成时,它会降低应用程序的性能并使应用程序工作缓慢? 在这种情况下部分垃圾收集已完成,何时主要的垃圾收集是否进行?


解决方案 System.gc()经常工作。当他们说你不能强制垃圾回收时,人们的意思是,JVM比你更了解内存状态,如果JVM知道现在不是一个好时机,你不能强制垃圾回收。



2)我不认为用户生成的类会将其转换为perm gen(尽管我可能是错的),它存储用于存储元信息,如类和interned字符串(pre Java 7)等等,这些都是JVM所需要的。


3)静态变量是由 class 引用的。 '宣布了。类存储在永久代中,所以它们本身的静态变量总是被引用的,所以它们也可以在perm gen中使用。

4)是的。 p>

从评论中编辑:
永久生成永远不会完成垃圾收集。对不对?

不完全。垃圾收集很复杂!烫发与其余的堆相比波动性要小得多,而且很有可能那里的物体会引用较低空间中的其他物体。我认为垃圾收集和转换的行为取决于你使用的Java版本,我相信新版本也会垃圾收集转发代码,这很有意义,因为Java使得代理对象的使用变得非常多。


I know lots of question have been asked about Garbage Collection and I have gone through them, but I still have have some doubts.

  1. If we cannot force the JVM for Garbage collection then what is the need of System.gc()? In which scenario it is useful?

  2. I know about young generation [eden,SO,S1] and old generation and how objects are moved from young generation to old generation. When will an Object be moved to permanent generation? For example, I have mycar object that has reference and is not eligible for garbage collection so when will be mycar object will move in permanent generation?

  3. I have read that static variables are stored in permanent generation. When they will be garbage collected and what type of other objects are stored in permanent generation? Why are static variables stored in permanent generation and what is the use of permanent generation?

  4. I know objects are stored in heap memory. Is this true that every application has its own heap memory?

  5. Is it true that Calling System.gc() reduces application performance and slows down our application? Or whenever garbage collection is done by JVM it reduces application performance and can make our application work slowly?

  6. In which cases is partial Garbage Collection is done and when is Major Garbage Collection performed?

解决方案

1) System.gc() works more often than not. What people mean when they say you can't force garbage collection is that that JVM knows more about the state of memory than you, you can't force garbage collection if the JVM knows it's not a good time to do it.

2) I don't believe user generated classes will make it into perm gen (although I could be wrong), it exists to store meta information such as classes and interned strings (pre Java 7) etc which are always required by the JVM.

3) Static variable are references by the class they're declared on. Classes are stored in permanent generation so by their very nature static variable will always be referenced so it makes sense to also have them in perm gen.

4) Yes.

Edit from comments: Garbage collection is never done on permanent generation. Am i right?

Not quite. Garbage collection is complicated! The perm gen is far less volatile than the rest of the heap and it's highly likely that the objects there will reference others in the lower spaces. I think the behaviour of garbage collection and perm gen is dependant on the version of Java you're using, I believe newer versions will also garbage collect the perm gen, which makes sense since Java makes a lot of use of proxy objects.

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

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