Thursday, June 30, 2005

Java - Memory Leaks

Now that we are talking Java, why not continue the discussion a little further, and since we ended on pointers in Java, the next question would be - what are memory leaks in Java, and why does it occur if Java performs its own garbage collection?

This is how the garbage collector in java works - starting at the root class and parsing through all nodes being referenced, at the same time keeping track of which objects are actively being referenced. Any classes no longer being referenced are then eligible to be garbage collected. What is to be noted is that an object is only counted as being unused when it is no longer being actively referenced. One other thing to keep in mind is that, unlike other languages such as C++, allocated memory is returned to the operating system when the application is closed.

Some common causes of memory leaks -
* Register a class as a listener, without unregistering it when it is no longer needed
Until the application receives event notification, the JVM will not garbage collect the storage.
* Declare a huge collection to be static
Collections hold other reference objects, and making them static keeps them in memory throughout the lifetime of the application.
* Not closing database or network connections
It is very important that the database or network collection is released once the information exchange is completed.
* Including native code in a Java program
The storage for references created in native C or C++ coding environments is not handled or garbage collected by the JVM. Hence the JNI memory must be directly dereferenced by the native language it was written in.

Now I know why I keep forgetting things nowadays. Lots of static HashMaps and Vectors pervading my brain!

Java - Unchecked Exceptions

There are two types of Exceptions in Java - The Checked and the Unchecked. Ever wondered why we have this classification? Any exception that can be thrown by a method is part of the method’s API and forms an integral part of that method’s declaration as are its parameters and return value. Just imagine the complexity of a method declaration if it would make all exceptions necessary to be declared! And worse, some of these exceptions would never occur in a program if the program was written carefully and diligently.

Unchecked exceptions, on the other hand, represent problems resulting from a programming problem, from which the application cannot recover or which cannot be handled in any way - for eg ArrayIndexOutOfBounds or NullPointerException.

Moral of the story - If the code can reasonably be expected to recover from an exception, make it a checked exception. If the code cannot do anything to recover from the exception, make it an unchecked exception.

Oh yes, on a related note - Are there pointers in Java? If your answer is NO, could you please explain why there are NullPointerExceptions everytime I run my code?!

Saturday, June 04, 2005

More inspiration quotes..

We make a living by what we get, we make a life by what we give.
- Winston Churchill

Live as if your were to die tomorrow. Learn as if you were to live forever.
- Gandhi

Just as a candle cannot burn without fire, men cannot live without a spiritual life.
- The Buddha

Life is like riding a bicycle. You don't fall off unless you plan to stop peddling.
- Claude Pepper

It is not length of life, but depth of life.
- Ralph Waldo Emerson

Expecting life to treat you well because you are a good person is like expecting an angry bull not to charge because you are a vegetarian.
- Shari R. Barr

And in the end, it's not the years in your life that count. It's the life in your years.
- Abraham Lincoln

There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.
- Albert Einstein

Attack life, it's going to kill you anyway.
- Steven Coallier

Friday, June 03, 2005

Cute little San Diego...

The memorial weekend was a welcome relief from the enormous work I was loaded with. So, my friends and I planned a quick trip to San Diego, situated in south of California. Had a look at Sea world and the zoo. There were a few scenic spots in the San Diego old town area. I also picked up a little stuff in the shops there.
The San Diego downtown is a great place to go to try out various types of cuisines - Chinese, Indian, Mexican, Thai, American, French ... the list if endless.
Missed out going to the Animal park and the Harbor there. Next time probably!