Thursday, August 25, 2005

MyArrayList Implementation

In this blog, the assignment was to show implementation of the set and get method for ArrayList. I tried to make but I arrived at this following code in which MyArrayList extends directly AbstractList. In MyArrayList, I used get and set method. This is not a dynamic actual custom implementation of ArrayList.


CODE:
import java.util.*;

public class TestArrayList{

private static class MyArrayList extends AbstractList
{
private Object a[];

MyArrayList(Object array[]) {
a = array;
}

public Object get(int index) {
return a[index];
}

public Object set(int index, Object element) {
Object oldValue = a[index];
a[index] = element;
return oldValue;
}

public int size() {
return a.length;
}
}

public static void main(String args[])
{
Object[] bArray= new Object[4];

bArray[0] =new Integer(12);
bArray[1] =new Integer(10);
bArray[2] =new Integer(14);
bArray[3] =new Integer(16);

MyArrayList aArray = new MyArrayList(bArray);

System.out.println("Previous to change \t" + aArray.get(1));
aArray.set(1,new Integer(40));

System.out.println("After changing value \t" +aArray.get(1));

System.out.println("Size of the array \t" +aArray.size());
}
}

OUTPUT:

Previous to change 10
After changing value 40
Size of the array 4

Thursday, August 11, 2005

Garbage Collection in JVM

The memory space which had been allocated to an object from the heap, can be reused when it is not being referenced by the original object. This means that once object finished its work, the memory space that had been used by the object can be reclaimed. This whole process of reclaiming memory is known as garbage collection.

Now more technically, the heap is where the objects of a Java program live. Any time you allocate memory with the new, newarray, anewarray, and multianewarray instructions, the memory comes from the heap. The Java language doesn't allow you to free allocated memory directly. Instead, the runtime environment keeps track of the references to each object on the heap, and automatically frees the memory occupied by objects that are no longer referenced this process called garbage collection.

Two type of garbage can be collected one is the pointer which is pointing to non existing memory address other one is memory space which doesn’t have any pointer to reference it. Also the problem with reclamation of memory arises due to restriction on physical memory size.

It can be called as memory recycling because the memory once freed can be used to allocate to new object. For this GC should have some mechanism to check out which are the object memory space not being referenced. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed. Finalizer is activity that will check for dirty object. If it is dirty then it will be written back else will simply freed.

GC has to cope with heap i.e. memory compaction or defragmentation of memory. Suppose memory already allocated in some random order from available heap. Now if memory freed which was lying in between some other object’s memory space, then it will be some what similar to OS memory management scenario. Where having enough memory space to allocate it to process doesn’t satisfy whole requirement. But due to no single memory segment available which is big enough to accommodate process, process waits in queue. This same thing can happen in dynamic memory allocation if proper memory compaction methods are not being used.

So it is clear that any garbage collector must do three basic things. First, it must detect garbage objects i.e. unreferenced objects. Second, it must reclaim the heap space used by the garbage objects and make the space available again to the program. Third, it must take care of possible memory fragmentation.

Memory Management in JVM

There are three type usually associated with VM.

1) Static memory management: - Here everything is allocated statically. All memory areas are allocated statically before execution begins. In this method the memory space for function parameter is also allocated before execution. This is not a good strategy for a VM implementation since it restricts use of recursive calls, it doesn’t allow multithreading. This strategy generally doesn’t allow any change to data structure at runtime.

2) Linear memory management: - Memory is allocated and freed in LIFO (Last In First Out) order. New object can be allocated space very easily. But for deleting old object’s memory space all new object’s memory space has to be deleted. This is used in very simple VMs. Since many data object in VM are managed in stack like structure.

3) Dynamic memory allocation: - Memory can be allocated in any order. Allocation takes place from special area which is known as heap. Memory once allocated is not fixed; size of the object can grow or shrink on the fly. Most of the modern VM use some form of the dynamic memory management. It can be either automatic or manual. Whenever programmer has responsibility of freeing the unused memory space explicitly like in c free (), this called as manual dynamic memory management. But if all allocation and de-allocation of memory done by VM without programmers intervention, this is called as automatic dynamic memory allocation.

3a) Automatic Dynamic Memory Management: - Programmers are free from all worry of allocation and de-allocation of object’s memory space. Garbage collector comes into picture in this method which reclaims unused (unreferenced) memory space. Memory system automatically expands and shrinks data in the heap as necessary. Benefits of this system include making programming easier, no dangling pointer and improve program reliability and safety. Implementation challenges for automatic memory management are keeping tracks of pointer to memory space, deleting memory space allocated to an object i.e. garbage collection, whether to compact heap whenever object memory space is deleted