Home of The JavaSpecialists' Newsletter

038bCounting Objects Clandestinely - Follow-up

Posted: 2001-12-28Category: PerformanceJava Version: Dr. Heinz M. Kabutz



When I wrote the last newsletter, I still had too much sea water in my head with the effect that I actually didn't write as much in terms of explanation as I should have. Consequently, some of you sent me questions that made me realise that a follow-up article would be appropriate:

Q: Why do you use a "boolean counting" variable?
A: I want to avoid counting objects that are being created as part of the counting process. My first approach was to store the objects in a fixed length array, but the problem is that before too long, that array would overflow. So, I want to tell the code: "Hold your horses, I'm going to count a bit". I hope that my code won't generate deadlocks, but I have a feeling that it might.

Q: How do you replace Object.class?
A: The way I used to do it in the past was to patch the rt.jar file. However, it is much easier to simply specify the path to the new Object.class file when you run your program. You can do that by calling java -Xbootclasspath/p:<path to where java/lang/Object.class is> Test You might find that this produces a security exception, someone has reported that from Weblogic. If it does, you might have to patch the jar file and resign it or something, I dunno.

Q: While the code you wrote helps find how many objects are created, your friend stated he wanted to know about memory leaks. The code does not indicate how many, of any object type, he has in memory at any one point in time. I was trying to work out how one would do that, thinking maybe you could override finalize to decrement the counter. But I suppose, unlike the constructor, you're not guaranteed of it being called. Do you think there might be a way to access some internal tables to find out what references exist, or something like that?
A: Something I tried, which kept me occupied for quite a while, was to implement finalize() in Object to decrease the counter. Sure, you still don't know what really needs to be on the heap, but if you call System.gc() a few times you should have a good chance. However, in JDK 1.3 and JDK 1.4 I got a core dump when I tried to put ANY code in the finalize() method of Object.

Q: I get error messages when I try to compile your Object file with Ant. What should I do?
A: Erik Hatcher: Ant definitely has its share of issues, and it might require you to do a fork="true" to spawn a new VM in order to compile something like Object as it has some convoluted and sometimes in-the-way classloader issues. Heinz Kabutz: Alternatively, compile by just calling javac directly.

In this country we used to celebrate the 1st and the 2nd of January as holidays. The 1st was a day of heavy drinking on the beach (with subsequent work for the lifesavers :-(, and the 2nd was the day to sleep off the babbelas (hangover) of the 1st. For some reason, our new government stopped having the 2nd as a holiday, but most pragmatic businesses still take the 2nd off. Most sensible folk (like me ;-) avoid the beach on the 1st, so today is a holiday for me without the unpleasantness of a hangover combined with a sunstroke.

Kind regards



Related Articles

Browse the Newsletter Archive

About the Author


Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.