sujith
sujith

Reputation: 675

Strings and Permgen memory

I have a map of format Map stored in a file. This file has over 100,000 records.

The value of each entry is nearly 10k.

I load 1000 records into a map in memory , process them ,then clear the map and load the next 1000 records.

My question is :

  1. Since the strings are stored in String pool which is in permgen memory area , when i clear the map will the Strings be garbage collected ?

  2. Incase if they are not garbage collected is there any way to force them to be garbage collected?

  3. Is there any guarantee that if the program is running out of memory , JVM would clean the permGen memory before throwing OutOfMemory Exception ?

Upvotes: 1

Views: 413

Answers (1)

TheLostMind
TheLostMind

Reputation: 36304

Ok.. Let's start....

Since the strings are stored in String pool which is in permgen memory area , when i clear the map will the Strings be garbage collected ?

All strings are NOT stored in String constants pool. Only interned Strings and String literals go into the String constants pool. There is no concept of permgen in java-8. Metaspace has (almost gracefully) replaced Permgen.

If you have Strings read from a file (which are not interned), then yes your strings will get GCed. If you have String literals (and God save you if you do.. :P), the they will be GCed when the classloader which loaded your class which defined these string literals gets GCed.

Incase if they are not garbage collected is there any way to force them to be garbage collected?

Well, You could always call System.gc() explicitly (NOT a good idea in production environment). If you are using java-8 use G1Gc and enable String deduplication.

Is there any guarantee that if the program is running out of memory , JVM would clean the permGen memory before throwing OutOfMemory Exception

The GC will try its best to cleanup as much as it can. No, there is no guarantee that this would happen.

Upvotes: 4

Related Questions