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(“”));

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) {






                 * @throws IOException

                 * @throws FileNotFoundException


                private static void loadProp() throws IOException, FileNotFoundException {

                                Properties prop = new Properties();

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



I also wrote a small shell script to check the behavior



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

        sleep 1;


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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: