Java memory areas in java 8

2020-02-29 18:43发布

问题:

I have read a lot about java memory areas, but looks like it is just a mess. Mostly due to the introduction of a new MetaSpace area instead of PermGen in java8. And there are questions now:

  1. What areas does the heap include in java8+ ?
  2. Where the static methods and variables are stored before java8 and java8+ ?
  3. Does the MetaSpace store anything except class meta-data info ?
  4. Does the structure of memory areas depend on the implementation of JVM ?

Thank you for your answers.

回答1:

  1. Does the structure of memory areas depend on the implementation of JVM ?

Absolutely. PermGen or Metaspace are just implementation details of a particular JVM. The following answers are about HotSpot JVM, the reference implementation of Java SE virtual machine.

  1. What areas does the heap include in java8+ ?

For the above reason it would be more correct to say "in JDK 8" rather than "in Java 8".

The structure of Java Heap depends on the selected GC algorithm. E.g. with Parallel GC and CMS the heap is divided into Old and Young generations, where the latter consists of Eden and two Survivor Spaces.

G1 Heap is divided into the regions of the same size. Epsilon GC heap is a single monolithic area. And so on.

  1. Where the static methods and variables are stored before java8 and java8+ ?

Methods (both static and non-static) reside in Metaspace in JDK 8 or in PermGen prior to JDK 8. Not sure what you mean by "variables": field values are in Java Heap, the field metadata (names, types, offsets) is in Metaspace.

  1. Does the MetaSpace store anything except class meta-data info ?

The following items are stored in Metaspace:

  • Classes (their internal representation)
  • Symbols (names and signatures)
  • Primitive arrays (e.g. field metadata is represented as an array of unsigned shorts)
  • Methods and their bytecodes
  • Method counters
  • Constant pools and CP caches
  • Annotations

All of these can be considered "class metadata".