Welcome 2009

It all started in the Golden Gate Bridge of San-Francisco. We had exiting (official) days in solving some of the long pending issues. Then there comes a big change in my career, as I perceive.  I went to take challenges in a new way. New challenges gave me opportunities to do thing that a hard-core developer implore to do. Continue reading

Anonymous IO streams

 

I love to do method chaining in my code, though I frequently hit with the following problems

1.       While doing debugging “Skip Over” will skip a line of source. It doesn’t treat each method call separately. I remember there is way in Netbeans to do this. Comment on this blog, if you know the way to do in Eclipse. I usually use “Inspect” in these cases

2.       It is not possible to make checks on the outcomes of a particular call. To the worst case it is not even possible to log the outcome of a particular call

Today , I was badly hit by a problem which seems to be a file handle leak in the code that is completely written by me. When I juggle through the code I found a line

                Properties prop = new Properties();

                prop.load(new FileInputStream(“velocity.properties”));

Stopped over for a minute, and gave a thought “Is anonymous inner streams are closed by itself?” I have crossed this question nearly hundred times, but I don’t have a firm answer. Today I made my mind to evaluate on this. I used the following program to evaluate this

                public class JavaProperties {

                public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {

                                while (true) {

                                                loadProp();

                                                Thread.sleep(1000);

                                }

                }

                /**

                 * @throws IOException

                 * @throws FileNotFoundException

                 */

                private static void loadProp() throws IOException, FileNotFoundException {

                                Properties prop = new Properties();

                                prop.load(new FileInputStream(“velocity.properties”));

                }

}

I also wrote a small shell script to check the behavior

                while(true)

do

        echo `pfiles $1 | grep velocity  | wc -l`;

        sleep 1;

done;

I started the program in background and keep observing the number of file handles opening. As expected, the file handle keeps increasing with time. To the surprise suddenly, the handle count dropped to 0.

 I suspected that GC would have run at that point of time. So I restarted the java program after enabling GC debug (-verbose:gc -XX:+PrintGCDetails). This clearly shows that the handle is cleared when GC runs.

But when I create a local variable and explicitly close the stream handle count stays at 0. Though JVM takes care of cleaning up the handles during GC, it is better to close the handle explicitly. Having a local variable is not going to really cost much.