Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing memory consumption in Java [closed]

I have worked with some projects where achieving better performance and reducing memory consumption has been the primary goal. But believe me it has already been hard to control the memory consumption in Java.

This wiki page, says on "Java Program Speed" Java is in some cases equal to C++ on low-level and numeric benchmarks.

These statistics do not really show a major difference in time computation Performance benchmark. However, the memory usage in Java is quite higher than C++, since there is an 8-byte overhead for each object and 12-byte for each array in Java (32-bit; twice as much in 64-bit java) as mentioned in above wiki link.

Now the question is what all measures people take to minimize the memory utilization in Java?

Please note that I am much concerned about memory than performance, since I cannot think of any better ways than "writing better programs" and "correctly tuning memory in JDK"

like image 587
Anshu Avatar asked Sep 24 '12 04:09

Anshu


People also ask

How do I reduce Java memory usage?

Set the Heap Size If you reduce the Java heap size by a certain amount you will reduce the memory footprint of the Java process by the same amount. You can however not reduce the Java heap size infinitely. The heap must be at least large enough for all objects that are alive at the same time.

How do you fix memory limit exceeded in Java?

Memory Limit Exceeded Error: It typically occurs when no memory limit has been set. It means that the program is trying to allocate more memory than the memory limit for the particular problem. For Example, if the memory limit is 256 MB, then there is no need to write code that requires more than 256 MB of memory.

Does Java automatically clear memory?

Java garbage collection is the process by which Java programs perform automatic memory management. The garbage collector find unreferenced objects and deletes them to free up memory. Java garbage collection is an automatic process. The programmer does not need to explicitly mark objects to be deleted.


1 Answers

Depending on your what you are working on you can benefit from different programming practices such as:

  • Limiting the amount of variables that you initialize way before actually using them.
    • Just a general good coding tip. A variable that has been initialized and isn't needed for awhile is holding on to memory that isn't needed yet which may be wasting resources.
  • Object caching via static objects where applicable.
    • I've used this before in reflection heavy applications where I need to get the list of members on an object within a loop and it can help performance considerably depending on the situation where it may be needed.
  • Object pooling where applicable
    • http://www.javaworld.com/jw-06-1998/jw-06-object-pool.html
  • If you are writing a multi-threaded application you can utilize thread pooling in order to gain performance by limiting the amount of work in creating and launching new threads.
    • http://www.ibm.com/developerworks/library/j-jtp0730/
like image 171
AdamM Avatar answered Oct 05 '22 03:10

AdamM