Lecture 07 10 Garbag Array
Lecture 07 10 Garbag Array
R. S. Yadav
Memory Allocation
The JVM divides memory into stack and heap memory.
Stack Memory in Java is used for static memory allocation and the execution of a thread.
It contains primitive values that are specific to a method and references to objects
referred from the method that are in a heap.
These objects have global access and we can access them from anywhere in the application.
We can break this memory model down into smaller parts, called generations, which are:
1. Young Generation – this is where all new objects are allocated and aged. A minor
Garbage collection occurs when this fills up.
2. Old or Tenured Generation – this is where long surviving objects are stored. When
objects are stored in the Young Generation, a threshold for the object's age is set, and
when that threshold is reached, the object is moved to the old generation.
3. Permanent Generation – this consists of JVM metadata for the runtime classes and
application methods.
Memory Allocation
Key Features of Java Heap Memory
● It's accessed via complex memory management techniques that include the Young
Generation, Old or Tenured Generation, and Permanent Generation.
● If heap space is full, Java throws java.lang.OutOfMemoryError.
● Access to this memory is comparatively slower than stack memory
● This memory, in contrast to stack, isn't automatically deallocated. It needs Garbage
Collector to free up unused objects so as to keep the efficiency of the memory usage.
● Unlike stack, a heap isn't threadsafe and needs to be guarded by properly synchronizing
the code.
Memory Allocation
class Person {
int id;
String name;
• Reference Counting
• Mark and Sweep
How Objects are Created in Java
• allocate memory;
• assign fields their default values;
• run the constructor;
• a reference is returned.
How Java Reclaims Objects Memory
– Easier programming
Ali Object
Garbage: unreferenced objects
– Reference counting
– Mark-and-sweep
Reference Counting Garbage Collection
if (p!=q)
Example:
{ Object p = new Integer(57);
if (p!=null) Object q= new Integer(99);
--p.refCount; p=q
p=q;
p
if (p!=null) 57
++p.refCount;
refCount = 0
}
q
99
refCount = 2
Reference Counting (cont'd)
• Reference counting will fail whenever the data
structure contains a cycle of references and the cycle
is not reachable from a global or local reference
• Disadvantages
– Reference counting does not detect garbage with cyclic
references.
– The overhead of incrementing and decrementing the
reference count each time.
– Extra space: A count field is needed in each object.
– It may increase heap fragmentation.
Mark-and-Sweep Garbage Collection
• The mark-and-sweep algorithm is divided into
two phases:
– Mark phase: the garbage collector traverses the
graph of references from the root nodes and marks
each heap object it encounters. Each object has an
extra bit: the mark bit – initially the mark bit is 0. It is
set to 1 for the reachable objects in the mark phase.
– Sweep phase: the Garbage collection scans the heap
looking for objects with mark bit 0 – these objects
have not been visited in the mark phase – they are
garbage. Any such object is added to the free list of
objects that can be reallocated. The objects with a
mark bit 1 have their mark bit reset to 0.
Mark and Sweep (cont'd)
• Advantages
– It is able to reclaim garbage that contains cyclic
references.
– There is no overhead in storing and manipulating
reference count fields.
– Objects are not moved during Garbage collection – no
need to update the references to objects.
• Disadvantages
– It may increase heap fragmentation.
– It does work proportional to the size of the entire
heap.
– The program must be halted while garbage collection
is being performed.
Four States of Memory Chunks
1. Free = not holding an object; available
for allocation.
2. Unreached = Holds an object, but has
not yet been reached from the root set.
3. Unscanned = Reached from the root
set, but its references not yet followed.
4. Scanned = Reached and references
followed.
21
Mark and Sweep Algorithm
Garbage Detection
Depth-first search to mark live data
cells (cells in heap reachable from
.variables on run-time stack)
Garbage Reclamation
Sweep through entire memory,
putting unmarked nodes on freelist.
Sweep also unmarks the marked
.nodes
22 L9GC ceg860 (Prasad)
Mark-and-Sweep
Basic idea
go through all memory and mark every
chunk that is referenced
make a second pass through memory and
remove all chunks not marked
OS 0 1 2 3
p2 = 650 p2 = 360
0 10 35 45 60
0 0 0 0
root A 0 B 0 C 0 D 0 E 0 F 0
free
:After mark
root A 1 B 0 C 1 D 0 E 0 F 1
free
:After sweep
root A 0 B 0 C 0 D 0 E 0 F 0
free
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
double[] myList = new double[10];
myList reference
myList[0] 5.6
myList[1] 4.5
Array reference myList[2] 3.3
variable
myList[3] 13.2
myList[4] 4
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34
myList[7] 45.45
myList[8] 99.993
myList[9] 11123
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
26
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
27
Creating Arrays
arrayRefVar = new datatype[arraySize];
Example:
myList = new double[10];
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
28
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
29
arrayRefVar.length
For example,
myList.length returns 10
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
30
Default Values
When an array is created, its elements are
assigned the default value of
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
31
Array Initializers
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
32
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
33
Processing Arrays
1. (Initializing arrays)
2. (Printing arrays)
3. (Summing all elements)
4. (Finding the largest element)
5. (Finding the smallest index of the
largest element)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
34
You still have to use an index variable if you wish to traverse the array in a
different order or change the elements in the array.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
35
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
36
Two-dimensional Arrays
// Declare array ref var
dataType[][] refVar;
// Alternative syntax
dataType refVar[][] = new dataType[10][10];
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
37
double[][] x;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
38
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
matrix.length? 5 array.length? 4
matrix[0].length? 5 array[0].length? 3
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
39
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
40
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
41
array[4].length ArrayIndexOutOfBoundsException
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
42
Ragged Arrays
Each row in a two-dimensional array is itself an array.
So, the rows can have different lengths. Such an
array is known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5}, matrix.length is 5
{2, 3, 4, 5}, matrix[0].length is 5
{3, 4, 5}, matrix[1].length is 4
matrix[2].length is 3
{4, 5}, matrix[3].length is 2
{5} matrix[4].length is 1
};
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
43
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6
44
Note: You are required to write the program in java using object oriented concept where a
small functionality is required to be used as member function of a class. Further, data are
represented form of array.
8.Merge two ascending order sorted integer array into descending order sorted array.
9.Merge two sorted integer array where first one is in ascending order while second one in
descending order into ascending order sorted array.
10.Sort an integer array in ascending order using merge sort.
11.You are required to compute the following activities on two dimensional integer array.
i. Transpose the array.
ii. Sum the diagonal elements.
iii. Print the upper triangular of the array.
iv. add the two arrays.
v. multiply two arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All
rights reserved. 0-13-222158-6