Doubts on Garbage Collection in java

前端 未结 2 1946
长发绾君心
长发绾君心 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.

提交回复
热议问题