JVM PermGen – where art thou?
Join the DZone community and get the full member experience.Join For Free
the jvm is just another process running on your system and the magic begins with the java command. like any os process, it needs memory for its run time operations. remember – the jvm itself is a software abstraction of a hardware on top of which java programs run and boast of os independence and wora (write once run anywhere)
quick coverage of the jvm memory structure
as per the spec, jvm is divided into 5 virtual memory segments.
- method (non heap)
- jvm stack
- native stack
- pc registers
- every object allocated in your java program requires to be stored in the memory. the heap is the area where all the instantiated objects get stored. yes – blame the new operator for filling up your java heap ;-)
- shared by all threads
- the jvm throws java.lang.outofmemoryerror when it’s exhausted
- use the -xms and -xmx jvm options to tune the heap size
- eden (young) – new object or the ones with short life expectancy exist in this area and it is regulated using the -xx:newsize and -xx:maxnewsize parameters. gc (garbage collector) minor sweeps this space
- survivor – the objects which are still being referenced manage to survive garbage collection in the eden space end up in this area. this is regulated via the -xx:survivorratio jvm option
- old (tenured) – this is for objects which survive long garbage collections in both the eden and survivor space (due to lingering references of course). a special garbage collector takes care of this space. object de-alloaction in the tenured space is taken care of by gc major
- also called the non heap area (in hotspot jvm implementation)
- it is divided into 2 major sub spaces
permanent generation – this area stores class related data from class definitions, structures, methods, field, method (data and code) and constants. can be regulated using -xx:permsize and -xx:maxpermsize. it can cause java.lang.outofmemoryerror: permgen space if it runs out if space
code cache – the cache area is used to store compiled code. the compiled code is nothing but native code (hardware specific) and is taken care of by the jit (just in time) compiler which is specific to the oracle hotspot jvm
- has a lot to do with methods in the java classes
- stores local variables and regulates method invocation, partial result and return values
- each thread in java has its own (private) copy of the stack and is not accessible to other threads.
- tuned using -xss jvm option
- used for native methods (non java code)
- per thread allocation
- program counter specific to a particular thread
- contains addresses for jvm instructions which are being exceuted (undefined in case of native methods)
so, that’s about it for the jvm memory segment basics. coming to back to the permanent generation.
so where is permgen ???
essentially, the permgen has been completely remove d and replaced by another memory area known as the metaspace
metaspace – quick facts
- it’s part of the native heap memory
- can be tuned using -xx:metaspacesize and -xx:maxmetaspacesize
- clean up initiation driven by xx:metaspacesize option i.e. when the metaspacesize is reached.
- java.lang.outofmemoryerror: metadata space will be received if the native space is exhausted
- the permgen related jvm options i.e. -xx:permsize and -xx:maxpermsize will be ignored if present
this was obviously just the tip of the iceberg. for comprehensive coverage of the jvm, there is no reference better than the specification itself :-)
you can also explore
Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.