Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Method area still present in Java 8?

Prior to Java 8 we had 5 major runtime data areas:

  1. Method Area
  2. Heap
  3. JVM Stacks
  4. PC registers
  5. Native method stacks

With Java 8, there is no Perm Gen, that means there is no more “java.lang.OutOfMemoryError: PermGen”

which is great but I also read

Method Area is part of space in the Perm Gen

but I can't seem to find anything which explicitly says Method area is no more in Java 8.

So is Perm Gen along with Method area got removed or only Perm Gen got removed and Method area is still present in old generation.

Please attach any good source material that you may have seen related to Java 8 Memory Model

like image 343
Eshu Avatar asked May 03 '18 20:05

Eshu


People also ask

Is PermGen removed in Java 8?

Due to the above problems, PermGen has been completely removed in Java 8. In the place of PermGen, a new feature called Meta Space has been introduced. MetaSpace grows automatically by default. Here, the garbage collection is automatically triggered when the class metadata usage reaches its maximum metaspace size.

Is method area and Metaspace same?

The method area, also known as Permanent Generation (PermGen), is considered a part of Heap memory, logically, although the simpler implementations of JVM may choose not to garbage-collect it. However, Java 8 removes the PermGen space and introduces a new native memory space named Metaspace.

Where is method area in Java?

It contains per-class elements like constant pool, fields, method local data, method code, constructor codes etc. which are used in class and initialization of objects/interfaces. This method area gets created during JVM start-up. It is generally part of Heap area.

Why Pergem space is not there in Java 8?

PermGen space is replaced by MetaSpace in Java 8. The PermSize and MaxPermSize JVM arguments are ignored and a warning is issued if present at start-up. Most allocations for the class metadata are now allocated out of native memory. * The classes that were used to describe class metadata have been removed.


2 Answers

Since Method Area is a logical concept described in the specification, every JVM has a Method Area, though that doesn’t imply that it has to be reflected in the implementation code. Likewise, the Java Heap Space is specified as a concept in the specification, to be the storage of all Java objects, therefore all Java objects are stored in the Heap, per definition, regardless of how it is actually implemented.

Unlike the Perm Gen, which contained Java objects and JVM data structures other than Java objects, the memory layout of the HotSpot JVM for Java 8 has a clear separation. The Old Gen still only contains Java objects, whereas the Metaspace only contains JVM specific data and no Java objects. So Java objects formerly stored in the Perm Gen have been moved to the Old Gen. Since the Method Area contains artifacts “such as the run-time constant pool, field and method data, and the code for methods and constructors…”, in other words non-Java-objects (the pool may contain references to heap objects though), it is part of the Metaspace now.

You could now discuss whether the Metaspace is an implementation of Method Area or may contain more than the Method Area, but this has no practical relevance. Practically, the JVM contains code to manage the Metaspace and its contained artifacts and does not need to care whether these artifacts do logically belong to what the specification describes as “Method Area” or not.

like image 139
Holger Avatar answered Oct 16 '22 08:10

Holger


Here is the runtime data storage for HotSpot VM In Java 8

Heap

  • Has got all your objects created using new, including String constant pool
  • Contains your fields/instance variables

MetaSpace(Method Area)

  • Contains static data(Class variables and static methods)
  • Data in here is accessible by Heap, JVM stack
  • Unlike <=Java7 PermGen which takes JVM process memory which is limited and can't be expanded at runtime. MetaSpace uses native memory

JVM Stack

  • Current execution of your program.
  • Contains local variables
  • It's a thread

Native Stack

  • Used for native method executions, as Java core language has some native stuff
  • It's also a thread

PC register/ Instruction Sets

  • Holds the JVM memory addresses(Not Native address) for each JVM instruction in your stack
  • Generally each entry in JVM/native stack refers to PC registers for addresses to get actual data from Heap/MetaSpace
  • Each stack is associated with a PC register
like image 24
srk Avatar answered Oct 16 '22 08:10

srk