5

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?

J Richard Snape
20.4k5 gold badges55 silver badges85 bronze badges
asked Aug 26, 2016 at 6:35
5
  • 1
    4 ) Yes. Every application has its own heap memory. U can check this by running two diff java application in same system & provide max & mean heap size to it. Commented Aug 26, 2016 at 6:48
  • 1
    5) It depends on which type of GC is happening .. If it is a Full GC then stop the world will happen which can stop your application for a Full gc time. This will not happen in case of partial GC. Commented Aug 26, 2016 at 6:51
  • so in which case partial and major gc is performed Commented Aug 26, 2016 at 6:54
  • 3
    Those should be separate questions in my opinion. Commented Aug 26, 2016 at 8:19
  • Calling System.gc() from a library is always work, as the library writer knows nothing about the application. Calling it from the application is nearly always wrong as the GC understands its job better than most programmers. There are cases when it helps. Commented Sep 13, 2016 at 12:39

2 Answers 2

3

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.

answered Aug 26, 2016 at 6:44
Sign up to request clarification or add additional context in comments.

6 Comments

what are the other things which are stored in permanent generation?
String are stored in permenant Generation?
Interned strings, although apparently not anymore. This used to be the case but changed in Java 7, I'll update my answer!
Garbage collection is never done on permanent generation. Am i right? Because it contains meta data
That sounds like a good scenario to use System.gc();, I think genuine use-cases for this are quite rare though.
|
1

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.

answered Aug 26, 2016 at 8:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.