0% found this document useful (0 votes)
244 views

4.memory Management in Java

The document discusses memory management in Java. It describes how Java uses a garbage collector to manage memory in the heap rather than requiring manual memory management like in C/C++. The heap is divided into static and dynamic sections. The static heap contains immutable class definitions while the dynamic heap contains objects and references to methods in the static heap. When an object is no longer referenced, the garbage collector can reclaim its memory. The garbage collector runs periodically as needed to free up space in the heap.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views

4.memory Management in Java

The document discusses memory management in Java. It describes how Java uses a garbage collector to manage memory in the heap rather than requiring manual memory management like in C/C++. The heap is divided into static and dynamic sections. The static heap contains immutable class definitions while the dynamic heap contains objects and references to methods in the static heap. When an object is no longer referenced, the garbage collector can reclaim its memory. The garbage collector runs periodically as needed to free up space in the heap.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Memory Management in Java

Objectives
• Study Stack, Static Heap, Dynamic Heap
• Allocation and Deallocation
• Garbage Collection
Memory Management in Java
 Review: In C, 4 basic regions: Data segment (for global data),
code segment (for statements), stack (for local data of
functions when they are called), heap (for dynamic data).
C/C++ programmers must explicitly manage the heap of a
program.
 How Java heap is managed? (Refer to:
http://docs.oracle.com/javase/specs/)
– JVM supports the garbage collector in order to free Java
programmers from explicitly managing heap
– Java heap is managed by 2 lists: Free block list, Allocated block list
– Initial, free block list is all the heap
– After very much times for allocating and de-allocating memory,
fragmented and free blocks are not contiguous
Memory Management in Java
 How are data allocated in heap?
- Way: First fit
- If there is no blank block is fit, Java memory manager must compact
memory in order to create more larger free block
 Heap structure in Java
• Static heap contains class declarations  Invariable, garbage
collection is not needed
• Dynamic heap is divided into two sections: The first contains
objects and the second contains relations between object and
appropriate method in static heap. When an object is not used
(garbage), it’s memory can be de-allocated.
• When an object is created, a field for reference to the class declaration is
automatically added
• The next slide will depict it..
Memory Management in Java
Class1 Definition
-Constants/fields 1000
Dynamic
-Method table Fields heap
obj1:10000
(m1,,add1) 500 Section 1
(2)
(m2,,add2) (Garbage
obj2:8000
Fields
(4)
collection is
m1 applied)
m2 (3) (1)
1000
Class2 definition
-Constants/fields Dynamic heap Section 2 Dynamic
(5) Code of m1()
-Method table executes ( Entry: 2 references) heap
(10000, m1)  obj1.m1() Section 2
(m3,,add3)
Relations
(m4,,add4) (8000, m4)  obj2.m4() object-
m3 method
m4 500

Static heap
Garbage Collection
• Most modern languages permit you to allocate data
storage during a program run. In Java, this is done
directly when you create an object with the new
operation and indirectly when you call a method that
has local variables or arguments.
• Local data of a method include: return data,
parameters, variables are declared in the body of the
method.
• Local methods are allocated space on the stack and are
discarded when the method exits, but objects are
allocated space on the heap and have a longer lifetime.
Garbage Collection
• In Java, you never explicitly free the memory that
are allocated; instead, Java provides automatic
garbage collection.
• The runtime system keeps track of the memory
that is allocated and is able to determine whether
that memory is still useable.
• Garbage collector has the lowest priority. It runs
only when the system heap becomes exhausted.
• A data is treated as garbage when it is out of it’s
scope or an object is assigned to null.
Garbage Collection
Object obj1 = new Object();
int x= 5;
if (x<10) {
Object obj2= new Object();
Scope of a variable begins at the line
int y=3;
where it is declared and ends at the
closing bracket of the block
………
containing it
}
int t=7;
obj1 = null; obj2, y are out of scope ( they are
t*=8; no longer used)
……
obj1= null  Memory allocated to
obj1 is no longer used
Garbage Collection
When does garbage collector execute?
• Garbage collector has the lowest priority. So, it
runs only when program’s memory is
exhausted.
• It is called by JVM only. We can not activate it.

You might also like