views:

184

answers:

2

Hello to all and good day, 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.

+1  A: 

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.

djna
+5  A: 

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.

Bombe
+1 yes, surely there are tools for this
djna
Actually I am trying to optimize and I want to learn...I have finished my application and I am trying to make some measurements.
+1 for use a profiler. Checkout Visual VM (http://visualvm.dev.java.net)
Nick Holt
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.
Bombe
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.
Michael Borgwardt