Wednesday, June 3, 2015

Java Memory Leaks

Java memory Leaks can occur in the following ways:
  1. Saving an Object reference in to class level collection and forgetting to remove it at the proper time.
  2. Not Closing the JDBC Statement/ResultSets in a finally block.
  3. In native code, the Programmer forgetting to free a memory block.
To determine if the memory leak is caused by a Java memory leak, you should monitor the Java memory used.
The following code can be used to do this in the application:
System.out.println(“Java memory in use = “ + Runtime.getRuntime().totalMemory() –
        Runtime.getRuntime().freeMemory() );

No comments: