JVM PermGen – where art thou?

This post covers some basics of JVM memory structure and quickly peeks into PermGen to find out where it has disappeared since advent of Java SE 8

Bare Basics

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.

  • Heap
  • Method (non heap)
  • JVM Stack
  • Native Stack
  • PC Registers

 

jvm-memory-segments

 

Heap

  • 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

 

out-of-memory-error

 

Sub-divided into

  • 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

Method Area

  • 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

JVM Stack

  • 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

Native Stack

  • Used for native methods (non Java code)
  • Per thread allocation

PC Registers

  • 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 removed 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 🙂

(Additional note – thanks to inputs by Jon Diamond)

Note: The JVM specification itself is a standard for vendors (who implement the spec to build their own JVM) to follow. It should not be used to figure out vendor specific features (e.g. SAP JVM might implement a spec feature differently than that of the Oracle HotSpot VM). The JVM specification provides scope for innovation by not enforcing each and every minor details w.r.t implementation. In fact, we are living in the age of Polyglot Languages – which compile to byte code supported by the JVM (Scala, Groovy, JPython, JRuby, Kotlin etc). But for most of us out there – it can be leveraged to gain a solid understanding of the Java fundamentals ranging from bytecode to class loading process

 

You can also explore

Cheers !!

About Abhishek

Loves Go, NoSQL DBs and messaging systems
This entry was posted in Java, JVM and tagged , , , , , . Bookmark the permalink.

4 Responses to JVM PermGen – where art thou?

  1. Jon Diamond says:

    Hi Abhishek,

    Thanks for the article I love it when people look under the hood of the JVM it’s a very interesting place.

    I did however want to point out that you seem to have the wrong “specification” link, you point to the language spec and not the JVM spec which I think is what you intended to link to point to in the last sentence (see http://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf).

    It’s also worth pointing out to your readers that the JVM spec is somewhat vague when it comes to some of the finer points of JVM memory implementation (a good thing) so in this case it might not be the best reference 😉

    As I’m sure you know some JVM’s didn’t have the concept of permgen space at all (it’s not actually in the spec), the introduction of metaspace size makes the JVM more like the JRockit and IBM JVM’s but there are still differences and any other JVM implementation could also do things quite differently if it chose to.

    Cheers

    Jon

    Like

    • Abhishek says:

      Jon, first all of thanks for reading!

      1. I have made the correction for the link to the JVM spec
      2. Added a note for the reader (as per your suggestion)

      Appreciate your inputs ! 🙂

      Like

  2. Pingback: Execution of a Java Program – BUDDHIMA'S COMPUTER LAB

  3. Pingback: Java Virtual Machine – BUDDHIMA'S COMPUTER LAB

Leave a comment