Skip to main content


Showing posts from June, 2007

The right attitude for software development

A fledgling software developer with the right attitude? Photo credit: Mdf Note: This text was originally posted on my earlier blog at

Concluding the series on Java garbage collection

Over the past few blog posts we have covered some basics of garbage collection in Java. Garbage collection is a key strength of the JVM, since we do not have to worry about releasing object memory. It reduces memory leaks and other problems associated with improper memory management code that often creeps in when we program against hard deadlines :-) However, for large programs we very often have to tweak the default garbage collection mechanism to improve performance. Here's a nice article that explains garbage collection in much more detail with good examples. This page contains several links to memory management in the Java Hotspot VM. Here's another page that explains how to fine tune the Java garbage collector in Java 1.5. It is also a good idea to keep up with the latest in technology, so here's a page that describes enhancements to the Java VM in version 1.6, that influence the garbage collector. I hope you enjoyed this series. Over the next few months I hope to

Garbage collection example

A Java program can request the underlying JVM to perform garbage collection by calling the gc() method of the System class. Note that System.gc() is a request to the underlying JVM. Garbage collection may not always happen. Hence we cannot depend on this method, however we can use it to optimize performance, with the understanding that it may not work as desired on all JVM's.   Study the program shown below. It ilustrates how we can invoke the garbage collector from a Java program. 01   /** This program creates instances of Bag objects in a loop which will run 100000 times. 02     * Whenever the program is in a loop that is a multiple of 1000, it requests the JVM 03     * to start the garbage collector. We will see that the JVM may not always fulfill the 04     * request. 05     */ 06   public class  GarbageCollectionDemo  { 07      public static  void  main ( String args []) { 08        //loop 10000 times 09        for ( int  i= 0 ;i< 1

Adaptive algorithms

Adaptive algorithms do not use any fixed algorithm for garbage collection. They monitor the heap and choose an algorithm that is most effective for the current usage pattern. Such algorithms may also divide the heap into sub heaps and use a different algorithm for every sub heap, depending on the usage. Note: This text was originally posted on my earlier blog at

Generational algorithms

Introduction: Generational algorithms make use of a property found in most Java programs. Several studies have revealed a pattern in the way objects are used. It has been found that objects that get created early in program execution usually live longer than objects that get created later. It is usually the youngest objects that are garbage collected first. Look at the diagram below. The blue area in the diagram is a typical distribution for the lifetimes of objects. The X axis represents the bytes allocated by the JVM, and the Y axis represents th number of surviving bytes (live objects). The sharp peak at the left represents objects that can be reclaimed (i.e., have "died") shortly after being allocated. Iterator objects, for example, are often alive for the duration of a single loop. Image source: The above image has been taken from the document " Tuning garbage collection with the 5.0 Java[tm] virtual machine " If you notice, the distribution stret


Attended (and partly managed/mismanaged) BlogCampPune on Saturday. I am happy we organized an event for bloggers in Pune. This medium of expression and conversation has a lot of potential. People often point out that individual blogs (those who rant about toothpaste and the like) are doing more harm than good by cluttering the internet. But I think otherwise. There are real gems written by people who have some meaningful stuff to share. But besides sharing, a blog is a medium for individual expression. It's like having a conversation in the ether... you are generally talking to anyone who is interested... and anyone who is interested responds back. Writing also has the potential to guide a person towards personal development. Blogging also enables grassroots journalism. So, all in all, I think blogging is an excellent medium, and I'm glad BlogCampPune happened. Overall the unconference was pretty good. I admit, wi-fi sucked, as did the presentations by some people who did not

Copying algorithms

Copying algorithms are tracing algorithms that divide the heap into multiple regions; one object region in which objects are created, and one or more free space regions (which are ignored, ie. objects are never created in these regions). When the object space becomes full, all live objects are moved to the free space. Now the free space becomes the object space, and the old object space becomes the free space. Since objects are placed contiguously in the new region, holes (fragments) are not formed. The most commonly used copying algorithm is known as stop and copy, which uses one object region and one free space region. When the object region becomes full, the (Java) program is paused until live objects are copied into the free space. The program is restarted after the copying phase. Copying algorithms have an advantage over compacting algorithms in that the address of object references does not change when the objects are moved. Discuss this post in the learning forum .

May not be able to post this week

Hello Friends, I may not be able to post any new blogs this week. However, I should be able resume normal posting again by the weekend. Please bear with the lack of new learning material for a few days :-) See ya'll soon.  Note: This text was originally posted on my earlier blog at

Java discussions and screencasts for learning

From some time I have been thinking about publishing a series of podcasts/screencasts to help people learning Java. I would like to structure them as discussions (with professional Java developers) instead of lectures. These discussions will be structured with the aim to educate. Each session will be focussed on a particular Java concept or library. Things that would ideally constitute a session are: Brief explanation of the concept/library (say the Java IO library) Places where it can be applied Special situations to take care of Pain points Best practices These sessions will be augmented blog posts containing screencasts, code samples, exercises, and links to other learning resources. What do you guys think of such a series? Will it be helpful to developers? Do you have any suggestions? Note: This text was originally posted on my earlier blog at Here are the comments from the original post ----- COMMENT: AUTHOR: Sanket Daru URL: http://sankdadevi

Compacting algorithms

Tracing algorithms result in a fragmented heap, because objects that are garbage collected in the sweep phase, usually occupy arbitrary positions in the heap, resulting in holes, wherever the old objects were garbage collected. Compacting algorithms have a modified sweep phase, in which all live objects are copied into one end of the heap. After they are copied, the rest of the heap is freed. This places all live objects contiguously, and the heap is defragmented. Advantages: The memory is defragmented in the sweep phase Disadvantages: The [Java] program has to be paused when the garbage collector starts. References have to be updated when the live objects are moved. Discuss this post in the learning forum . Commercial Links Buy programming books from our powered storefront . Earn as you browse. Sign up for Algoco . Note: This text was originally posted on my earlier blog at