http://www.technicalpage.net/search/label/SQL

Showing posts with label Heap memory. Show all posts
Showing posts with label Heap memory. Show all posts

Heap Memory VS Stack Memory

Heap memory is managed by the JVM and is primarily used for objects and their instance data.
Stack memory is managed by the JVM and is used for method execution, including local variables and references.
For example:
int id = 10;
Car c = new Car();
 
Here “id” and “c” are stored in the stack memory.
 
“new Car()” ie the “car object “ is stored in the heap memory.
stack memory are generally smaller and faster compared to heap memory.
Stack memory is managed automatically by JVM for method execution, where as Heap memory is managed by the JVM at runtime. It stores dynamically created objects, and the garbage collector reclaims memory from objects that are no longer needed.
In a nutshell,
Stack → method execution and local variables/references
Heap → objects and their instance data