Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doubts on Garbage Collection in java

Tags:

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?

like image 856
FaisalAhmed Avatar asked Aug 26 '16 06:08

FaisalAhmed


2 Answers

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.

like image 183
StuPointerException Avatar answered Sep 22 '22 16:09

StuPointerException


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.

like image 41
the8472 Avatar answered Sep 24 '22 16:09

the8472