0

I want to test how much memory takes a class(foo) in java.In the constructor of foo I have the followings new:

int 1 = new int[size]
int 2 = new int[size]
....
int 6 = new int[size]

The size begins from 100 and increases until 4000.
So my code is:

Runtime r  = Runtime.getRuntime();
for(int i=0;i<10;i++)
r.gc();

double before = r.TotalMemory()-r.freeMemory();
Foo f = new Foo();
double after = r.TotalMemory()-r.freeMemory();
double result = after-before;

The problem is unti 2000 I have an increasing good result.But after 2000 I have a number which is < of the result of 2000. I guess that the gc is triggered.And sometimes I get the same number as it doesn't see the difference.
I did run with -Xms2024m -Xmx2024m which is my full pc memory. But I get the same behaviour.
I did run with -Xmn2023m -Xmx2024m and I get some strange results such as: 3.1819152E7.

Please help me on this.Thanks in advance.

4
  • What language/platform are you using? Commented Aug 19, 2009 at 10:58
  • I tagged it as Java based on getRuntime() - feel free to correct if this is wrong. Commented Aug 19, 2009 at 10:58
  • Strange? You do realise that 3.1819152E7 is 31819152, i.e. ~31 MB? Commented Aug 19, 2009 at 11:00
  • if its java it should be r.totalMemory() to compile Commented Aug 19, 2009 at 12:48

2 Answers 2

6

All these “I need to know how much memory object A takes” question are usually a symptom of premature optimization.

If you are optimizing prematurely (and I assume that much) please stop what you’re doing right now and get back to what you really should be doing: completing the application you’re currently working on (another assumption by me).

If you are not optimizing prematurely you probably still need to stop right now and start using a profiler that will tell you which objects actually use memory. Only then can you start cutting down memory requirements for objects or checking for objects you have forgotten to remove from some collection.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually I am trying to optimize and I want to learn...I have finished my application and I am trying to make some measurements.
Alex, then use a profiler. All this messing around with Runtime.free/total/maxMemory() and the garbage collector will get you nowhere. Honestly. Don’t do it.
I would have done it if I didn't have the pressure from "above" to show results in diagrams etc...I hope to understand :)
Any decent profiler such as visualvm will give you lots of pretty diagrams.
1

Garbage collectors are clever beasts. They don't need to collect everything everytime. They can defer shuffling things around. You could read about Generational Gabage Collection.

If you want to know how much memory your class is taking, why bother to introduce undertainty by asking for garbage collection. Hold on the the successively bigger objects and examine how big your app gets. Look at the increments in size.

List myListOfBigObjects

for ( for sizes up to 100 or more ) {

    make an object of current size
    put it in the list

    now how big are we?
}

Or you could just say "an int is so many bytes and we have n x that many bytes" there's some constant overhead for an object, but just increasing the array size will surely increase the object by a predictable amount.

Comments

Your Answer

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