Doubts about garbage collection in java

I know that many questions were asked in the Garbage Collection, and I went through them, but I still have some doubts.

  • If we cannot force the JVM to collect garbage, then what is the need for System.gc() ? In which scenario is this useful?

  • I know about the young generation [eden, SO, S1] and the old generation, and how objects move from generation to generation. When will the object be transferred to a permanent generation? For example, I have a mycar object that has a link and is not allowed to collect garbage, so when will mycar be moved in a constant generation?

  • I read that static variables are stored in a constant generation. When will they collect garbage and what other objects are stored in a permanent generation? Why are static variables stored in the constant generation and what is the use of the constant generation?

  • I know that objects are stored in heap memory. Is it true that each application has its own heap memory?

  • Is it true that Calling System.gc() slows down application performance and slows down our application? Or whenever the garbage collector does the JVM, it slows down application performance and can make our application run slowly?

  • When is partial garbage collection done and when is the Major Garbage Collection performed?

+5
source share
2 answers

1) System.gc() works more often than not. What people mean when they say that you cannot force garbage collection is that the JVM knows more about the state of memory than you, you cannot force garbage collection if the JVM knows that this is not the right time for this .

2) I do not believe that user-created classes will turn it into perm gen (although I may 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) A static variable is a reference using the class that they are declared. Classes are stored in the constant generation, therefore, by their nature, a static variable will always be referenced, therefore it makes sense to also have them in perm gen.

4) Yes.

Edit comment: Garbage collection is never done on a permanent generation. I'm right?

Not really. Garbage collection is complicated! The permanent gene is much less volatile than the rest of the heap, and it is very likely that objects there will refer to others in the lower spaces. I think that the behavior of the garbage collection and perm gen depends on the version of Java you are using, I believe that new versions will also garbage collect perm gen, which makes sense, since Java uses proxy objects a lot.

+3
source

1) The often repeated statement is true and yet misleading. The specification of the method (i.e. Javadocs) is formulated in such a way that makes the noop implementation valid. In other words, there are no special guarantees that he is doing something or doing something asynchronously or something else.

But implementation can provide much stronger behavior. In other words, people have to say that System.gc is implementation and configuration dependent and, in some circumstances, runs the GC sequentially for each call.

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

3) They are not

4) "Application" is too fuzzy, you can run several applications on a common JVM. The JVM has one managed heap.

5) These are two separate issues.

6) Depends on the implementation of the JVM and the selected GC algorithm. I suggest you familiarize yourself with the documentation.

+1
source

Source: https://habr.com/ru/post/971822/


All Articles