Tuesday, July 24, 2018

JVM notes

What is stack and heap in JVM ?






How to avoid memory leaks:-

1) Do not hold heavy objects or collections in static variables as they prevent GC to kill the object.
2) Use StringBuffer or builder to concatenate the strings instead of using string as the later will keep filling the string pool which can prevent heap size to exhaust.
3) String is no more stored in permgen space as with java 8 it has been discontinued, so because of string pool, there will be no memory out of exception.
4) Unclosed streams, although with Java 8/7 we can use try with resource and it will auto close.
5) Unclosed connections
6) Start adding objects in HashSet without implementing the hashcode and equals method for these objects, as a result, all objects , even though some of them being equal will keep getting added.

How to inspect memory leakage:-

1) Good code review
2) -verbose:gc parameter to the JVM configuration 
3) Eclipse memory analyzer

No comments:

Post a Comment