Is Method area still present in Java 8?
Asked Answered
T

3

18

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

Tertia answered 3/5, 2018 at 20:16 Comment(2)
There's at least a Method Area in the spec.Postglacial
I removed the [memory-model] tag, as the memory model describes interactions of threads with the shared memory, which is a different thing than a memory layout.Doviedow
D
29

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.

Doviedow answered 4/5, 2018 at 7:59 Comment(6)
True agreed to what you said and also the point where this particular question can actually become a topic of discussion is also valid maybe a new memory layout image would make it much more clearer to understand the concept of Method Area.Tertia
since ‘Metapace’ is not mentioned in JVM Specification, so I think HotSpot's implementation is a little different to the JVMS,is there any article described the Run-Time Data Areas in HotSpot?Tuatara
@ericzhao wiki.openjdk.java.net/display/HotSpot would be a good starting pointDoviedow
Is that correct that in -- new String("a") -- the "a" literal will be stored in run-time constant pool that is in turn is part of Metaspace? So there will be one "a" value on the heap and another "a" in Metascpace?Tass
@Tass no, objects are always stored in the heap, by definition. The answer contains a link to the part of the specification which defines the heap. The constant pool is just a data structure consisting of references to objects. So, the literal "a" is represented by a String object in heap memory, while the constant pool has a reference to it, to ensure that all code locations containing the same literal will share that object. When you create a new string using new String("a"), it will also live in the heap space. Makes two objects, but only one being referenced by the pool.Doviedow
@Holger, thank you! I've been trying to figure that out for some time.Tass
G
9

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
Glossa answered 23/6, 2020 at 9:4 Comment(3)
Where are none static methods are located? Thanks!!Pentode
@BahramdunAdil non static methods are located in Stack.Nonparticipation
@Nonparticipation Then it means, non-static methods like local variables live in Stack? But the stack is a place where a method called will add to stack not when loaded? I mean like static methods that are live in metaspace, where does the non-static method go when JVM loads the classes?Pentode
D
0
  • Firstly, you need to distinguish what is definition and what is implementation.

  • "Method Area" is a logical concept in the JVM specification. It still exist in Java8。refer:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.4; while PermGen、MetaSpace are concepts of hotspot(is one implementation of jvm).

  • "PermGen elimination in JDK 8" which means in hotspot.

Donalt answered 23/3, 2023 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.