Javamex

Java discussion forum to accompany the Javamex web site.

Hi Neil,

first of all, thank you for all your efforts on javamex in general and with classmexer in particular.

Unfortunately I have to say that the results of classmexer (0.02b on jdk 1.5.0) are plainly wrong. After some experimentation, I found two major reasons:

1) classmexer does not traverse object arrays. This is a real bummer because it renders the results absolutely useless.

2) it obviously stores the already counted objects in some ordinary (Hash?)Set. Hence, two Objects that are equal are only counted once, which is wrong. IdentityHashMap would be your friend here.


Günter


BTW: I've also tested java.sizeOf found at Sourceforge, which seems to give proper results.



public class ClassmexerTest3 {

Integer i1;
Integer i2;

static void equalsTest() {

ClassmexerTest3 o = new ClassmexerTest3();

// i1 and i2 are the SAME object so they only count once

o.i1 = new Integer(100);
o.i2 = o.i1;

System.out.println("Same objects? " + (o.i1 == o.i2));
System.out.println(MemoryUtil.deepMemoryUsageOf(o, MemoryUtil.VisibilityFilter.ALL));


// i1 and i2 are NOT the SAME object, hence we should need more memory
// ... but we get the same memory usage as before

o.i1 = new Integer(100);
o.i2 = new Integer(100);

System.out.println("Same objects? " + (o.i1 == o.i2));
System.out.println(MemoryUtil.deepMemoryUsageOf(o, MemoryUtil.VisibilityFilter.ALL));

// with different values, the result is what one would expect

o.i1 = new Integer(100);
o.i2 = new Integer(200);

System.out.println("Same objects? " + (o.i1 == o.i2));
System.out.println(MemoryUtil.deepMemoryUsageOf(o, MemoryUtil.VisibilityFilter.ALL));
}


static void arrayTest() {
Long[] array = new Long[1000];

System.out.println("Empty array: " + MemoryUtil.deepMemoryUsageOf(array, MemoryUtil.VisibilityFilter.ALL));

for (int i = 0; i < 1000; i++) {
array[i] = new Long(i);
}

System.out.println("Filled array: " + MemoryUtil.deepMemoryUsageOf(array, MemoryUtil.VisibilityFilter.ALL));
}

public static void main(String[] args) {
arrayTest();
equalsTest();
}
}

Views: 240

Reply to This

Replies to This Discussion

Thanks I'll have a look at these issues.
Please take a look at the updated version of Classmexer I've posted which should resolve the issues you mention.

Reply to Discussion

RSS

© 2024   Created by Neil Coffey.   Powered by

Badges  |  Report an Issue  |  Terms of Service