JAVA-What are the Memory Allocations available in Java?

LearnNewTech
2 min readJan 16, 2022

--

Java has five significant types of memory allocations.

  • Class Memory
  • Heap Memory
  • Stack Memory
  • Program Counter-Memory
  • Native Method Stack Memory

Class Memory:

All executing threads share this part of the JVM memory area. Class elements like constant pool, class fields, constructor codes, method codes, etc. Method area can be considered as a part of the heap area but stores per-class data only. We can say that the method area is responsible for holding class level information

Heap Memory:

Heap Memory in java is used by java runtime to allocate memory to objects and class during a java program’s execution. Whenever an object is created in java, it gets stored into heap memory. A garbage collection process runs on heap memory to free up unnecessary space that is garbage collection removes those objects from the heap area that does not have any references. Heap memory in java is divided into the following parts:

  • Young Generation: This is the part where all newly created objects are placed. When this part of the java heap gets filled up, a minor garbage collection occurs to free up space.
  • Old Generation: All objects left in memory after minor garbage collection are moved into the old generation. Therefore this is the part of heap memory where long-living objects are present.
  • Permanent Generation: This part of JVM contains native and static methods that provide metadata for running java applications.

Here are some important points regarding java heap memory:

  • If Heap space gets full, OutOfMemory error is thrown by java.
  • Access to Heap memory is slow as compared to stack memory.
  • Heap memory is much more in size as compared to stack memory.
  • Heap memory is not thread-safe as all objects share it.
  • Automatic deallocation is not present in heap memory as it needs a garbage collector to free up space.

--

--

No responses yet