Memory allocation and de-allocation is faster as compared to Heap-memory allocation. Stack-memory has less storage space as compared to 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.
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.
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.
Simply
For example:
Student std = new Student();
after executing the line above memory status will be like this.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With