Question

I'm using collection objects (Arraylist, hashmap mainly). My program is running 24*7. In between sometimes, it throws an exception out of memory Error: Java heap space. I have already given 1gb of space for JVM My Question is whether I need to use Global objects of Collection or local objects for each methods?

(Almost 1000000 data I have to process per day continuously 24*7)

Was it helpful?

Solution 4

You should use local variable until and unless it is being used across the methods. and Try to make Global variable null whenever its value is not going to use anymore. But you should be more sure while making object null. These null objects get garbage collected easily, which helps you to avoid memory exceptions. Also review your code for infinite loops while iterating collections, arrays etc.

OTHER TIPS

You could also set the heap space to 2GB and see if the problem still occurs. Thats the poor mans memory leak detection process. Otherwise use a profiler like VisualVM and check for memory leaks.

You can use a source code quality tool like Sonar.

You can also use Eclipse Memory Analysis tool. It enables you to analyse the heap dump & figure out which process is using the maximum memory. Analyze productive heap dumps with hundreds of millions of objects, quickly calculate the retained sizes of objects, see who is preventing the Garbage Collector from collecting objects, run a report to automatically extract leak suspects.

I always use it to fix OutOfMemory exceptions.

All the answer were really helpful : 1.When the requirement of the program is to run 24*7 then use local variable across the method. 2.The program must be thread safe(if Thread is used). 3.Use Connection pooling because when your connection object is used in infinite loop then creating & destroying every time is a big performance issue , so always make 10 or 15 connection in the beginning & checkout and checkin the connection. 4. Use Memory Analysis tool to analyse the heap dump & figure out which process is using the maximum memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top