Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heap vs Stack vs Perm Space

  • What are the differences between the Java memory spaces (Perm Space, Space Stack, Heap Space)?
  • When does the JVM use one or another?
  • If I use Scala/Groovy/etc., are there differences?
like image 893
caarlos0 Avatar asked Jul 23 '11 16:07

caarlos0


People also ask

Which has more storage heap or stack?

Memory allocation and de-allocation is faster as compared to Heap-memory allocation. Stack-memory has less storage space as compared to Heap-memory.

What is difference between stack and heap memory?

Overview. Stack memory is the space allocated for a process where all the function calls, primitive data types (int, double, etc.) and local and reference variables of the functions are stored. On the other hand heap memory is used to store the objects that are created during the execution of a Java program.

What is perm gen space in heap?

A java. lang. OutOfMemoryError: PermGen Space is a runtime error in Java which occurs when the permanent generation (PermGen) area in memory is exhausted. The PermGen area of the Java heap is used to store metadata such as class declarations, methods and object arrays.

Is heap memory smaller than stack memory?

Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. As soon as the method ends, the block becomes unused and becomes available for the next method. Stack memory size is very less compared to Heap memory.


2 Answers

Simply

  • Heap space: All live objects are allocated here.
  • Stack space: Stores references to the object for variable in method call or variable instantiation.
  • Perm space: Stores loaded classes information

For example:

Student std = new Student(); 

after executing the line above memory status will be like this.

  • Heap: stores "new Student()"
  • Stack: stores information about "std"
  • Perm Space: stores information about Student class
like image 57
Kowser Avatar answered Sep 23 '22 08:09

Kowser


Forgive me for adding an answer to such an old question - The current answer is great, but misses a couple of edge cases because of static code and Java 8 updates.

Overview

  • Stack
    • Allocated per Thread
    • Stores local references and primitives
    • This is scoped memory - When a method or thread ends, all their data in the stack is lost
    • Has the fastest access, so a local primitive is faster to use than a local Object
  • Heap
    • All allocated object instances exist here
    • Divided into Generations, with the youngest generation being the first place GC looks
    • Available to all threads so allocations and deallocations should be synchronised
    • This memory can become fragmented (but you do not usually manage this yourself)
  • PermGen
    • Stores loaded class information
    • Stores immutable information (Primatives, interned Strings)
    • Stores static class members

Example Code

public class SimpleVal { //The Class (loaded by a classloader) is in the PermGen      private static final int MAGIC_CONSTANT = 42; //Static fields are stored in PermGen     private static final SimpleVal INSTANCE = new SimpleVal(1); //Static field objects are created in the heap normally, with the reference in the PermGen ('class statics' moved to the heap from Java 7+)     private static SimpleVal previousInstance; //Mutable static fields also have their reference in PermGen so they can easily cause memory leaks      private int value; //Member variables will be part of the heap      public SimpleVal(int realValue) {         value = realValue;         ...     }      public static int subtract(SimpleVal val1, SimpleVal val2) {          ....     }      public int add(SimpleVal other) { //Only one copy of any method (static or not) exists - in PermGen          int sum = value + other.value; //Local values in methods are placed in the Stack memory          return sum;     }  }  public static void main(String[] args) {      SimpleVal val1 = null;     SimpleVal val2 = new SimpleVal(3); //Both of these variables (references) are stored in the Stack       val1 = new SimpleVal(14); //The actual objects we create and add to the variables are placed in the Heap (app global memory, initially in the Young Gen space and later moved to old generation, unless they are very large they can immediately go old gen)      int prim = val1.add(val2); //primitive value is stored directly in the Stack memory     Integer boxed = new Integer(prim); //but the boxed object will be in the heap (with a reference (variable) in the Stack)      String message = "The output is: "; //In Java 7+ the string is created in the heap, in 6 and below it is created in the PermGen     System.out.println(message + prim);  } 

Java 8 Note: PermGen space was replaced with what is called Metaspace. This still functions the same but can be resized automaticlly - by default Metaspace auto increases its size in native memory up to a maximum (specified in JVM params), but PermGen always has a fixed maximum size contiguous to the heap memory.

Android Note: From Android 4.0 (from 3.0 in practice) Android should honour the described memory contracts - but on older versions the implementation was broken. The 'Stack' memory in Android-Davlik is in fact register based (instruction sizes and counts vary between the two, but for a developer the functionality remains the same).

Finally, for more info the best answer I've ever seen to this subject on StackOverflow is here

like image 33
Nick Cardoso Avatar answered Sep 21 '22 08:09

Nick Cardoso