Memory heap and stack

All objects are stored on the heap (including their attributes).1

Local variables (including arguments) always contain primitive values or references and are stored on the stack.

String one = "abc";
String two = new String("abc");
You'll have two objects on the heap (two String objects containing "abc") and two references, one for each object, on the stack (provided one and two are local variables).

There are two storage areas involved: the stack and the heap. The stack is where the current stateof a method call is kept (ie local variables and references), and the heap is where objects are stored. The Hotspot documentation says that on Linux 64-bit each thread has a stack of 1024kB by default. The heap can be made arbitrary big, and today it's in the order of GB.
A recursive method uses both the stack and the heap. Which one you run out of first depends on the implementation. As an example, consider a method which needs thousands of integers: if they are declared as local variables, ie:
public void stackOverflow() { int a_1; int a_2; int a_3; // ... int a_10_000_000;}

your program will crask with a StackOverflowError
. On the other hand, if you organize your integers in an array, like:
public void outOfMemory() { int[] integers = new int[10 * 1000 * 1000];}

the heap will be filled soon, and the program will end with an OutOfMemoryError
. In neither case the memory is corrupted or data overridden. However, in both cases the code is wrong and must be fixed somehow - but to tell you how we'd need to know more about your program.

Deque (Stack) as object is stored on the heap

你可能感兴趣的:(Memory heap and stack)