Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Counting Class Instantiations

0 views
Skip to first unread message

Luc The Perverse

unread,
Jan 12, 2008, 6:50:40 PM1/12/08
to
I am trying to improve performance in a game I have written.

I was wondering if Java has a built in feature that could be easily
enabled to count unique class instantiations.

I am concerned in my functions which execute 100 times a second (well at
least they are supposed to) that I am needlessly recreating objects over
and over and over again. Perhaps for simple data this is not a large
consideration (a single string 1 time) - but for instance, I was doing
something like creating a unique "Rect" (Rectangle class) for every
sprite in the entire board, every frame iteration to check for
collisions. Since there are between hundreds and thousands of blocks
which make a board - I was needlessly creating millions of objects per
second - which then had to be garbage collected.

I am not asking for game engine specific optimization techniques - that
would no doubt be a question best suited for the GTGE forums.

I just want to see a count of instances classes which were instantiated
or garbage collected - and if anything ever hits 100 million then
perhaps I can raise a flag to check for it.

Even better would be instantiation count from unique position in the
call stack - so it wouldn't be such a needle in a haystack.

Does such a mechanism exist?

Thank you

--
Luc The Perverse

Miss Elaine Eos

unread,
Jan 12, 2008, 8:48:14 PM1/12/08
to
In article <hb0o55-...@loki.cmears.id.au>,

Luc The Perverse <ataylor_no_spa_am@_letterC_solutions.n3t> wrote:

> I was wondering if Java has a built in feature that could be easily
> enabled to count unique class instantiations.

I don't know if there's anything built in, but you could always make
everything derive from:

class DebugObject extends Object // NOT checked for syntax!
{
private static int objectCount = 0;

DebugObject ()
{
super();
++objectCount;
}

protected void finalize()
{
objectCount--;
super.finalize();
}

public int objectCount()
{
return (objectCount);
}
}


If you want to simplify switching back & forth, you can also have:

class JavaObject extends Object
{
JavaObject()
{
super();
}
}


Then it's pretty simple to do replace-all in your project. (If you
replace-all to "Object", it can get trickier to go back to DebugObject,
depending on the rest of your project.)

If *ONLY* javac had a preprocessor! ;)

--
Please take off your pants or I won't read your e-mail.
I will not, no matter how "good" the deal, patronise any business which sends
unsolicited commercial e-mail or that advertises in discussion newsgroups.

0 new messages