0% found this document useful (0 votes)
1 views4 pages

w4 MemoryAllocation

The document explains memory allocation in Java, detailing the roles of Stack and Heap memory. Stack memory is used for static allocation and is thread-safe, while Heap memory is for dynamic object allocation and is managed by garbage collection. It also describes how garbage collection works and the methods to make objects eligible for it, along with examples of memory management in Java.

Uploaded by

manichinniah94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

w4 MemoryAllocation

The document explains memory allocation in Java, detailing the roles of Stack and Heap memory. Stack memory is used for static allocation and is thread-safe, while Heap memory is for dynamic object allocation and is managed by garbage collection. It also describes how garbage collection works and the methods to make objects eligible for it, along with examples of memory management in Java.

Uploaded by

manichinniah94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction

Memory allocation in Java specifies the mechanism where the computer programs and
services are assigned dedicated virtual memory spaces. The Java Virtual Machine splits the
memory into Stack and Heap Memory.

Stack Memory
In Java, stack memory is used for static memory allocation and thread execution.

Methods, local variables, and reference variables are all stored in the Stack portion of
memory.

Since they are interpreted in a Last-In-First-Out way, the values in this memory are
temporary and restricted to particular methods. When a new method is created, a new
block is generated on top of the stack that includes values specific to that method, such as
primitive variables and object references.

When a method completes its execution, the resulting stack frame is cleared, the flow
returns to the calling method, and space for the next method becomes available.

Characteristics of Stack memory

1. It expands and contracts as new methods are called and returned.


2. Variables within the stack only last as long as the method's scope remains.
3. When the method is executed, it is properly allocated and deallocated.
4. Java throws java.lang.StackOverFlowError if this memory is full.
5. Since each thread runs in its own stack, this memory is thread-safe.
6. As compared to heap memory, access to this memory is fast.

Heap Memory
Any time an object is created and allocated in Java Heap Space, it is used. In heap memory,
new objects are often formed, and references to these objects are stored in stack memory.
Garbage Collection, a discrete function, keeps flushing the memory used by previous objects
that have no reference. A Heap Space object can have unrestricted access throughout the
program.

These objects are accessible from anywhere in the program and have global access.

This memory model is divided into generations, which are as follows:


 Young Generation – All new objects are allocated to and aged in this memory. When
this is complete, a minor garbage collection occurs.
 Old or Tenured Generation – This is the memory where long-lasting items are kept.
When objects are stored in the Young Generation, an age threshold is set, and when
that threshold is met, the object is transferred to the Old Generation.
 Permanent Generation – This is a collection of JVM metadata for runtime classes and
application methods.

Characteristics of Heap memory


1. Complex memory storage methods such as Young Generation, Old or Tenured
Generation, and Permanent Generation are used to access it.
2. Java throws java.lang.OutOfMemoryError, if the heap memory is full.
3. Access to this memory is slower than access to stack memory.
4. Unlike the stack, this memory is not immediately deallocated. Garbage Collector is
used to free up unused objects in order to maintain memory efficiency.
5. A heap, unlike a stack, is not thread-safe and must be protected by synchronizing the
code properly.
6. Only a reference is created in Java when we only declare a variable of a class type
(memory is not allocated for the object). We must use new() to assign memory to an
object. As a result, the heap memory is always assigned to the object.

Example:

class Test {
void show() {
System.out.println("Inside Test::show()");
}
}

public class Main {


public static void main(String[] args) {
// all objects are dynamically allocated
Test t = new Test();
t.show(); // No error
}
}

Output:
Inside Test::show()

NOTE: Here 't' is a reference variable that refers to the test object that is in the heap
memory.
Garbage Collection in Java

Garbage collection in Java is a process of destroying runtime unused objects. Garbage


collectors destroy the objects automatically. A garbage collector's key goal is to allow
effective use of memory.

Ways to make an object eligible for the garbage collector


There are primarily three ways to make an object eligible for garbage collection.
 Nullifying the reference variable
Student obj = new Student();
obj = null;
 By assigning a reference variable to another.
Student obj1 = new Student();
Student obj2 = new Student();
obj1 = obj2;
 By anonymous object.
new Student();

Ways for requesting JVM to run garbage collector


There are two ways for requesting a JVM to run a garbage collector.
1. Using System.gc() method.
2. Using Runtime.getRuntime().gc() method.

Example:
public class Test
{
public static void main(String[] args) throws ExceptionInterrupted
{
Test t1 = new Test();
Test t2 = new Test();

// Nullifying the reference variable


t1 = null;

// requesting JVM for running Garbage Collector


System.gc();

// Nullifying the reference variable


t2 = null;

// requesting JVM for running Garbage Collector


Runtime.getRuntime().gc();

@Override
// finalize method is called on object once
// before garbage collecting it
protected void finalize() throws Throwable
{
System.out.println("Garbage collector called");
System.out.println("Object garbage collected : " + this);
}
}

Output:
Garbage collector called
Object garbage collected : Test@6fe026ef
Garbage collector called
Object garbage collected : Test@6127c563

You might also like