Doubts on Garbage Collection in java

前端 未结 2 1947
长发绾君心
长发绾君心 2021-02-10 03:11

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 fo

相关标签:
2条回答
  • 2021-02-10 03:50

    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.

    0 讨论(0)
  • 2021-02-10 04:07

    1) The often repeated statement is true and yet misleading. The specification of the method (i.e. the javadocs) are phrased in a way that make a noop implementation valid. In other words, there are no spec-guarantees that it does anything or only do something asynchronously or whatever.

    But implementations can provide much stronger behavior. In other words, what people should be saying is that System.gc is implementation- and configuration-dependent and under some circumstances does consistently trigger a GC on every call.

    2) Perm gen is an implementation detail of the hotspot JVM prior to java 8.

    3) They aren't

    4) "Application" is too fuzzy, you could run multiple applications on a shared JVM. There is one managed heap per JVM.

    5) Those are two separate questions.

    6) Depends on the JVM implementation and the chosen GC algorithm. I suggest you read the documentation.

    0 讨论(0)
提交回复
热议问题