Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. In this article, we shall learn about memory allocation in Java and we will discuss Stack and Heap Memory.
Java Stack memory is used for the execution of a thread. They contain method-specific values that are short-lived and references to other objects in the heap that is getting referred from the method.
Stack memory is always referenced in LIFO (Last-In-First-Out) order. 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.
Apart from what we have discussed so far, following are some other features of Stack memory:
import java.io.*; import java.util.*; class Test{ static void stack_push(Stack<Integer> stack){ for(int i = 0; i < 5; i++){ stack.push(i); } } static void stack_pop(Stack<Integer> stack){ System.out.println("Pop :"); for(int i = 0; i < 5; i++){ Integer y = (Integer) stack.pop(); System.out.println(y); } } static void stack_peek(Stack<Integer> stack){ Integer element = (Integer) stack.peek(); System.out.println("Element on stack top : " + element); } static void stack_search(Stack<Integer> stack, int element){ Integer pos = (Integer) stack.search(element); if(pos == -1) System.out.println("Element not found"); else System.out.println("Element is found at position " + pos); } public static void main (String[] args){ Stack<Integer> stack = new Stack<Integer>(); stack_push(stack); stack_pop(stack); stack_push(stack); stack_peek(stack); stack_search(stack, 2); stack_search(stack, 6); } }
//Output
Now, Let us move into Heap Space.
The memory is allocated during the execution of instructions written by programmers. Note that the name heap has nothing to do with heap data structure. It is called heap because it is a pile of memory space available to programmers to allocated and de-allocate. If a programmer does not handle this memory well, a memory leak can happen in the program.
Based on the above explanations, we can easily conclude the following differences between Heap and Stack memory.
PARAMETER | STACK | HEAP |
Basic | Memory is allocated in a Contiguous Block | Memory is allocated in a Random Order |
Allocation and Deallocation | Automatic by compiler | Manual by Programmer |
Cost | Less | More |
Implementation | Hard | Easy |
Access time | Faster | Slower |
Main Issue | Shortage of Memory | Memory Fragmentation |
Locality of Difference | Excellent | Adequate |
Flexibility | Fixed-Rate | Resizing is possible |
With this, we come to an end of this “Memory Allocation in Java” Tutorial. I hope you have understood the concept and its implementation through some real-time examples.
Now that you have understood Memory Allocation in Java basics through this “Memory Allocation in Java” article check out the Java Online Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification courses are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Mention it in the comments section of this “Memory Allocation in Java” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co