2

I have a small java test class with only one member variable.. and that variable is a String. I have an ArrayList into which I add a lot of objects of this class. I see that the heap used is ~6 times the data that I add to it). Is there any way to optimize this OR is using an ArrayList a problem in this case.

code:

public class  testheap
{
String          regionId;   

testheap(String s) {  
    this.regionId = s;
}


public static void main(String[] args) throws Exception
{

    ArrayList<testheap> regList = new ArrayList<testheap>(10000);

    System.out.println("just Before looping");
    printHeapSizes();
    //looping
    int i = 0;
    while (i++ < 2500000)   {
        if (i%500000 == 0) printHeapSizes();  // print heap sizes every 500000th iteration
        testheap reg = new testheap("abcd");
        regList.add(reg);
    }
    System.out.println("end of loop");
    printHeapSizes();
 public static void printHeapSizes() {
 long heapSize = Runtime.getRuntime().totalMemory(); 

    // Get maximum size of heap in bytes. The heap cannot grow beyond this size.// Any attempt will result in an OutOfMemoryException.
    //long heapMaxSize = Runtime.getRuntime().maxMemory();

     // Get amount of free memory within the heap in bytes. This size will increase // after garbage collection and decrease as new objects are created.
    long heapFreeSize = Runtime.getRuntime().freeMemory(); 
    long usedSize = heapSize - heapFreeSize;
    System.out.println("total:"+heapSize+" Freesize:"+heapFreeSize + "      USED:"+usedSize);

End of code

3
  • class names should be capitalized. Commented Nov 29, 2013 at 16:20
  • First of all, print out GC stats as well, they might shed some light on what's going on. Secondly, create a heap dump and analyse it using eclipse.org/mat (for example, but you can use jvisualvm as well, if you prefer that). Commented Nov 29, 2013 at 16:22
  • One thing I spotted though is that your ArrayList is created with a capacity of 10000, so it'll have to be resized several times to accommodate 250000 elements, and each resize might potentially mean copying stuff. That may cause a temporary peak in memory consumption, GC should remove the extra with time though. Commented Nov 29, 2013 at 16:24

1 Answer 1

2

You should only consider the size AFTER a full gc. I suggest you do a System.gc() first.

I would expect adding a simple object like this to use about 28 bytes. 4 bytes for the reference in the ArrayList, and 24 bytes for the testheap object itself (16 byte header, 4 bytes for reference and 4 bytes for padding)

The String doesn't use any space after the first one as it will use the same object each time.

If you care about memory usage and you know the size of list you want, use that size from the start.

List<TestHeap> regList = new ArrayList<TestHeap>(2500000);
Sign up to request clarification or add additional context in comments.

5 Comments

As I pointed out in my comment, ArrayList resizing might be behind all of this, so a full GC will probably clear out everything. It has to be a full GC because I suspect that due to the rapid creation of objects, eden space fills up and spills over into the tenured area.
@biziclop An ArrayList will not cause more than a ratio of 4x in the worst case has it always doubles the size as it grows. i.e. the current size will be no more than the sum of all the previous sizes.
We don't really know though what footprint OP expects the list to have :)
@biziclop or what the OP is getting. ;)
I don't have any educated expectation - but I was hoping that the heap size needed would be under 50% more than sum of sizes of objects being added. So, I am looking for another approach to store a huge list of objects created from an input file.... where heap reqmt would be less than twice payload size. The program as it stands now is taking nearly 6x times sum of object sizes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.