Stack and Heap memory in Java
Heap Memory vs Stack Memory
The Java Heap and Stack Memory model specifies how and when different threads can see values written to shared variables by other threads, and how to synchronize access to shared variables when necessary. Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM. The basic difference between stack and heap is the life cycle of the values. Stack values only exist within the scope of the function they are created in. Once it returns, they are discarded. Heap values however exist on the heap. They are created at some point in time, and destructed at another either by Garbage Collector or manually.
Java Stack Memory
Stack memory is a special region of your computer's memory that stores temporary variables created by each function. It contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Every time a function declares a new variable, it is "pushed" onto the stack . When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. Then every time a function exits, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a last in first out (LIFO) order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.
- The stack grows and shrinks as functions push and pop local variables
- There is no need to manage the memory yourself, variables are allocated and freed automatically
- Stack has size limits
- Stack variables only exist while the function that created them, is running
Java Heap Memory
Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. It is the runtime data area from which the Java Virtual Machine allocates memory for all class instances and arrays. Whenever create any object, it's always created in the Heap space. Unlike the stack , there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. Any object created in the heap space has global access and can be referenced from anywhere of the program. Java Garbage Collector is an automatic memory management system that reclaims heap memory for objects.
- Variables can be accessed globally
- No limit on memory size
- Relatively slower access
- No guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed
- Java Interview Questions-Core Faq - 1
- Java Interview Questions-Core Faq - 2
- Important features of Java
- What is the difference between JDK and JRE?
- What gives Java its 'write once and run anywhere' nature?
- What is JVM and is it platform independent?
- What is Just-In-Time (JIT) compiler?
- What is the garbage collector in Java?
- What is NullPointerException in Java
- How to set the maximum memory usage for JVM?
- What is numeric promotion?
- Why do we need Generic Types in Java?
- What does it mean to be static in Java?
- What are final variables in Java?
- How Do Annotations Work in Java?
- How do I use the ternary operator in Java?
- What is instanceof keyword in Java?
- How ClassLoader Works in Java?
- What are fail-safe and fail-fast Iterators in Java
- What are method references?
- "Cannot Find Symbol" compile error
- Difference between system.gc() and runtime.gc()
- How to convert TimeStamp to Date in Java?
- Does garbage collection guarantee that a program will not run out of memory?
- How setting an Object to null help Garbage Collection?
- How do objects become eligible for garbage collection?
- How to calculate date difference in Java
- Difference between Path and Classpath
- Is Java "pass-by-reference" or "pass-by-value"?
- Difference between static and nonstatic methods java
- Why Java does not support pointers?
- What is package in Java?
- What are wrapper classes?
- What is singleton class in Java?
- Difference between Local Variable, Instance Variable and a Class Variable in Java?
- Can a top level class be private or protected in java
- Are Polymorphism , Overloading and Overriding similar concepts?
- How to Use Locks in Java
- Why Multiple Inheritance is Not Supported in Java
- Why Java is not a pure Object Oriented language?
- Why can't a Java class be declared as static?
- Difference between Abstract class and Interface in Java
- Why do I need to override the equals and hashCode methods in Java?
- Why does Java not support operator overloading?
- What’s meant by anonymous class in Java?
- Static Vs Dynamic class loading in Java
- Why am I getting a NoClassDefFoundError in Java?
- How to generate random integers within a specific range in Java
- What's the meaning of System.out.println in Java?
- What is the purpose of Runtime and System class?
- What is finally block in Java?
- What is difference between final, finally and finalize?
- What is try-with-resources in java?
- What is a stacktrace?