How do you make your Java application memory efficient?
Posted
by Boune
on Stack Overflow
See other posts from Stack Overflow
or by Boune
Published on 2009-04-25T15:26:24Z
Indexed on
2010/04/06
6:43 UTC
Read the original article
Hit count: 221
How do you optimize the heap size usage of an application that has a lot (millions) of long-lived objects? (big cache, loading lots of records from a db)
- Use the right data type
- Avoid java.lang.String to represent other data types
- Avoid duplicated objects
- Use enums if the values are known in advance
- Use object pools
- String.intern() (good idea?)
- Load/keep only the objects you need
I am looking for general programming or Java specific answers. No funky compiler switch.
Edit:
Optimize the memory representation of a POJO that can appear millions of times in the heap.
Use cases
- Load a huge csv file in memory (converted into POJOs)
- Use hibernate to retrieve million of records from a database
Resume of answers:
- Use flyweight pattern
- Copy on write
- Instead of loading 10M objects with 3 properties, is it more efficient to have 3 arrays (or other data structure) of size 10M? (Could be a pain to manipulate data but if you are really short on memory...)
© Stack Overflow or respective owner