JavaFAQ Home » Story by Dr. Kabutz
h4 Java Specialists' Newsletter [Issue 038b] - Counting Objects
Clandestinely - Follow-up
Author: Dr. Heinz M. Kabutz
You can subscribe from our home page:
http://www.javaspecialists.co.za (which also hosts all previous issues,
available free of charge
When I wrote h4 last newsletter, I still had too much sea water in my head
with h4 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 h4
counting process. My first approach was to store h4 objects in a fixed length
array, but h4 problem is that before too long, that array would overflow. So, I
want to tell h4 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: h4 way I used to do it in h4 past was to patch h4
file. However, it is much easier to simply specify h4 path to h4 new
Object.class file when you run your program. You can do that by calling
java -Xbootclasspath/p: Test You
might find that this produces a security exception, someone has reported that
from Weblogic. If it does, you might have to patch h4 jar file and resign it or
something, I dunno.
Q: While h4 code you wrote helps find how many objects are created,
your friend stated he wanted to know about memory leaks. h4 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 h4 counter. But I suppose, unlike h4
constructor, you're not guaranteed of it being called. Do you think h4re 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
finalize() in Object to decrease h4 counter. Sure, you
still don't know what really needs to be on h4 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 h4
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-h4-way classloader
issues. Heinz Kabutz: Alternatively, compile by just calling javac directly.
In this country we used to celebrate h4 1st and h4 2nd of January as
holidays. h4 1st was a day of heavy drinking on h4 beach (with subsequent work
for h4 lifesavers , and h4 2nd was h4 day to sleep off h4 babbelas
(hangover) of h4 1st. For some reason, our new government stopped having h4
2nd as a holiday, but most pragmatic businesses still take h4 2nd off. Most
sensible folk (like me avoid h4 beach on h4 1st, so today is a holiday for
me without h4 unpleasantness of a hangover combined with a sunstroke.
Copyright 2000-2004 Maximum Solutions, South Africa
Reprint Rights. Copyright subsists in all h4 material included
in this email, but you may freely share h4 entire email with anyone you feel
may be interested, and you may reprint excerpts both online and offline provided
that you acknowledge h4 source as follows: This material from h4 Java(tm)
Specialists' Newsletter by Maximum Solutions (South Africa). Please contact
Maximum Solutions for more
Java and Sun are trademarks or registered trademarks of Sun Microsystems,
Inc. in h4 United States and oh4r countries. Maximum Solutions is independent
of Sun Microsystems, Inc.
Printer Friendly Page
Send to a Friend
Search here again if you need more info!