Java · Programming · reference

JVM Memory Model

There are six main memory areas in a JVM:

  1. Program Counter Register
  2. JVM Stack
  3. JVM Heap
  4. Method Area
  5. Runtime Constant Pool
  6. Native Method Stacks

Program Counter Register

  • Stores the address of the next bycode instruction.

JVM Stack

  • JVM stack is created per thread of execution, once the thread terminates the stack memory is reclaimed.
  • Stack size can be fixed or dynamic. Memory is allocated in Frames
  • JVM stack is for the storing the temporary data for a thread’s execution.
  • This memory is typically used for storing local variables, return values etc.
  • StackOverflowException is thrown when the maximum stack size is exceeded.
  • java.lang.OutOfMemoryError is thrown when the stack cannot be allocated more memory by JVM as system is out of memory.

JVM Heap

  • Heap is created at the JVM startup.
  • Heap is shared between all the runtime threads.
  • Heap can have fixed or dynamic size. Programmer can set the size of the heap at JVM startup.
  • This area is used to allocate Object and Arrays.
  • java.lang.OutOfMemoryError will be thrown if there is no more memory available in the system.

Method Area

  • Created at JVM startup.
  • Shared across all JVM threads.
  • It stores per-class structures like constructors code, method code, field and method data, interface etc.
  • java.lang.OutOfMemoryError will be thrown if there is no more memory available in the system.

Runtime Constant Pool

  • A runtime constant pool is a per-class or per-interface runtime representation of the constant_pool table in a class file.
  • t contains several kinds of constants, ranging from numeric literals known at compile time to method and field references that must be resolved at run time.
  • Each runtime constant pool is allocated from the Java virtual machine’s method area.
  • The runtime constant pool for a class or interface is constructed when the class or interface is created by the Java virtual machine.
  • When creating a class or interface, if the construction of the runtime constant pool requires more memory than can be made available in the method area of the Java virtual machine, the Java virtual machine throws an OutOfMemoryError.

Native Method Stacks

  • It is used as traditional stack.
  • Created for methods written in non-Java programming language.
  • StackOverflowException is thrown when the maximum stack size is exceeded.
  • java.lang.OutOfMemoryError is thrown when the stack cannot be allocated more memory by JVM as system is out of memory.

What are Frames?

A frame is used to store data and partial results, as well as to perform dynamic linking , return values for methods, and dispatch exceptions.
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are allocated from the Java virtual machine stack of the thread creating the frame. Each frame has its own array of local variables, its own operand stack, and a reference to the runtime constant pool of the class of the current method.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s