[ACCEPTED]-How do you make your Java application memory efficient?-objectpool

Accepted answer
Score: 20

I suggest you use a memory profiler, see 15 where the memory is being consumed and optimise 14 that. Without quantitative information 13 you could end up changing thing which either 12 have no effect or actually make things worse.

You 11 could look at changing the representation 10 of your data, esp if your objects are small. For 9 example, you could represent a table of 8 data as a series of columns with object 7 arrays for each column, rather than one 6 object per row. This can save a significant 5 amount of overhead for each object if you 4 don't need to represent an individual row. e.g. a 3 table with 12 columns and 10,000,000 rows 2 could use 12 objects (one per column) rather 1 than 10 million (one per row)

Score: 18

You don't say what sort of objects you're 6 looking to store, so it's a little difficult 5 to offer detailed advice. However some (not 4 exclusive) approaches, in no particular 3 order, are:

  • Use a flyweight pattern wherever possible.
  • Caching to disc. There are numerous cache solutions for Java.
  • There is some debate as to whether String.intern is a good idea. See here for a question re. String.intern(), and the amount of debate around its suitability.
  • Make use of soft or weak references to store data that you can recreate/reload on demand. See here for how to use soft references with caching techniques.

Knowing more about the internals 2 and lifetime of the objects you're storing 1 would result in a more detailed answer.

Score: 11

Ensure good normalization of your object 4 model, don't duplicate values.

Ahem, and, if 3 it's only millions of objects I think I'd 2 just go for a decent 64 bit VM and lots 1 of ram ;)

Score: 4

Normal "profilers" won't help 6 you much, because you need an overview of 5 all your "live" objects. You need 4 heap dump analyzer. I recommend the Eclipse Memory analyzer.

Check 3 for duplicated objects, starting with Strings. Check 2 whether you can apply patterns like flightweight, copyonwrite, lazy 1 initialization (google will be your friend).

Score: 3

Take a look at this presentation linked 4 from here. It lays out the memory use of 3 common java object and primitives and helps 2 you understand where all the extra memory 1 goes.

Building Memory-efficient Java Applications: Practices and Challenges

Score: 2

You could just store fewer objects in memory. :) Use 4 a cache that spills to disk or use Terracotta 3 to cluster your heap (which is virtual) allowing 2 unused parts to be flushed out of memory 1 and transparently faulted back in.

Score: 1

I want to add something to the point Peter 6 alredy made(can't comment on his answer 5 :() it's always better to use a memory profiler(check 4 java memory profiler) than to go by intution.80% of time it's 3 routine that we ignore has some problem 2 in it.also collection classes are more prone 1 to memory leaks.

Score: 1

If you have millions of Integers and Floats 4 etc. then see if your algorithms allow for 3 representing the data in arrays of primitives. That 2 means fewer references and lower CPU cost 1 of each garbage collection.

Score: 0

A fancy one: keep most data compressed in 5 ram. Only expand the current working set. If 4 your data has good locality that can work 3 nicely.

Use better data structures. The standard 2 collections in java are rather memory intensive.

[what 1 is a better data structure]

  • If you take a look at the source for the collections, you'll see that if you restrict yourself in how you access the collection, you can save space per element.
  • The way the collection handle growing is no good for large collections. Too much copying. For large collections, you need some block-based algorithm, like btree.
Score: 0

Spend some time getting acquainted with 5 and tuning the VM command line options, especially those concerning 4 garbage collection. While this won't change 3 the memory used by your objects, it can 2 have a big impact on performance with memory-intensive 1 apps on machines with a lot of RAM.

Score: 0
  1. Assign null value to all the variables which are no longer used. Thus make it available for Garbage collection.
  2. De-reference the collections once usage is over, otherwise GC won't sweep those.


Score: 0

1) Use right dataTypes wherever possible

Class Person {
 int age;
 int status;

Here 12 we can use below variables to save memory 11 while sending Person object

class Person{
  short age;
  byte status;

2) Instead of 10 returning new ArrayList<>(); from method 9 , you can use Collection.emptyList() which 8 will only contain only one element instead 7 of default 10;

For e.g

public ArrayList getResults(){
        return new ArrayList<>();
//Use this
public ArrayList getResults(){
       return Collections.emptyList();

3 ) Move creation of 6 objects in methods instead of static declaration 5 wherever possible as fields of objects will 4 be stored on stack instead of heap

4) Using 3 binary formats like protobuf,thrift,avro,messagepack 2 for reducing intercommunication instead 1 of json or XML

More Related questions