3

I was reading this article and it says

Permanent generation of Heap or Perm Area of Heap is somewhat special and it is used to store Meta data related to classes and method in JVM, it also hosts String pool provided by JVM as discussed in my string tutorial why String is immutable in Java. There are many opinions around whether garbage collection in Java happens in perm area of java heap or not, as per my knowledge this is something which is JVM dependent and happens at least in Sun's implementation of JVM. You can also try this by just creating millions of String and watching for Garbage collection or OutOfMemoryError

So I have one question:

Does garbage collection happens in Perm area of heap.if not then how static variables or methods and string literal pool gets garbage collected?

Fresher
  • 894
  • 1
  • 7
  • 27
  • The meaning of the different "areas" of heap is not writ in stone -- depends on the specific GC algorithms that your particular JVM uses. But any class (and the class's statics) loaded by the system or default class loader will never become eligible for collection and hence can be safely deposited in a "permanent" section of the heap. Classes loaded by other class loaders *are* potentially collectable. – Hot Licks May 14 '14 at 11:55
  • And, based on your excerpt, I doubt that that author knows what he's talking about. – Hot Licks May 14 '14 at 12:00
  • @HotLicks the author you are talking about is one of the best author of java on the web – Fresher May 14 '14 at 12:10
  • 1
    I'm sorry to hear that. – Hot Licks May 14 '14 at 12:29

2 Answers2

1

Garbage collection does happen in permgen space of heap too.

And static variables are garbage collected when their respective class loaders unload from memory.

String literal pool is probably never garbage collected.

EDIT If permgen is full, OutOfMemoryError : PermGen occurs.

codingenious
  • 8,385
  • 12
  • 60
  • 90
  • Firstly `String literal pool is probably never garbage collected.` So if it becomes full then what happens.Secondly Could you please give source of you answer. – Fresher May 14 '14 at 11:49
  • @Fresher - the "String literal pool" is not an "area" in memory, but is a conceptual classification for a collection of data. – Hot Licks May 14 '14 at 11:56
  • The "pool" of interned Strings is, in theory, collectable. There is a slight complication with regard to data synchronization, plus most interned Strings in "normal" circumstances are tied to loaded classes (which are mostly not collectable), so a JVM may or may not attempt to collect the pool in practice. – Hot Licks May 14 '14 at 12:05
  • @hotlicks `the "String literal pool" is not an "area" in memory, but is a conceptual classification for a collection of data`.I have heard that before but as far as I know they take memory in heap.Because you have to store somewhere the values assign to a String reference variable – Fresher May 14 '14 at 12:12
  • @Fresher - But the pool is not a specific area. Strings occupy heap space like any other object. – Hot Licks May 14 '14 at 12:30
  • @Batty so is there any solution to avoid `OutOfMemoryError : PermGen`. – Fresher May 14 '14 at 12:31
  • @HotLicks your both statements `String literal pool" is not an "area" in memory, but is a conceptual classification for a collection of data` and `But the pool is not a specific area. Strings occupy heap space like any other object` are actually contradictory. As far as I have read String literal pool takes heap memory.Could you plz give source of your answer. – Fresher May 14 '14 at 12:33
  • @Fresher Those statements aren't contradictory. Fresher's saying that the "String literal pool" isn't a distinguished/special area in memory. That does not mean that Strings aren't in memory, period. – awksp May 14 '14 at 12:50
1

Grabage collection do happen in permgen (Jvm implementation based). I wrote a answer to SO question how to deliberately fill perm gen. JVM loads metadata that is classes etc in permgen area. If for some reason JVM is running low on permgen it can decide to unload classes that have no references anywhere. But when that GC runs or if run at all may very well differ from JVM to JVM implementation.

Unloading classes to create room is the last ditch effort by a jvm before it throws "Outof memory: permgen space"

Please take a look at this Question SO Link this might be helpful Basically this shows how you can fillup permgen by preventing GC of permgen area.

Community
  • 1
  • 1
vkg
  • 1,839
  • 14
  • 15
  • So you agree with the author above? – Fresher May 14 '14 at 12:43
  • When it comes to strings This may vary from JVM to JVM. But all JVMs allocate a logical area in heap where it extracts and keeps all String literals from classes loaded in permgen. This is done so that only one copy is kept for String literals. See this other SO question as well for further understanding. http://stackoverflow.com/questions/4918399/what-type-of-memory-heap-or-stack-string-constant-pool-in-java-gets-stored – vkg May 14 '14 at 12:52
  • @vkg - "Logical" is the operative word. There is no "chunk" of heap that you can point to and say "this is the String pool". An interned String is (aside from the flag saying it's interned) indistinguishable from any other String in the heap. – Hot Licks May 14 '14 at 15:01
  • @HotLicks I think I would agree with you on this. But now cannot edit my comment. I would say see the link in my previous comment the answers over there explain very well. – vkg May 14 '14 at 15:46