| Author |
How can we count the number of objects created per class using Java1.5 API?
|
Jalli Venkat
Greenhorn
Joined: Aug 10, 2006
Posts: 27
|
|
Hi,
How can we count the number of objects created for a class using Java1.5 API..?
Is there any such API available...?
If not means , what is the solution for this....?
|
venkat
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 10043
|
|
|
Note that while this will tell you how many have been CREATED, that does not tell you how many are currently ALIVE. You could easily write a for loop that creates 100 objects and immediately discards the reference, making them all eligible for garbage collection. And you have no way to know when that may happen.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
The whole idea of using a high-level language is to avoid having to bother about such questions.
|
 |
Sidharth Pallai
Ranch Hand
Joined: Apr 21, 2008
Posts: 134
|
|
How to judge which objects are eligible for garbage collection. This code extract has been taken from Johnathan Giles java note. The explantion given by him is confusing me. Here it is ....
Even when e3 and e2 are set to be null, it is
not possible to do any garbage collection,
even though it feels like it should be possible
to garbage collect e2.
This is because e2 is still accessible from e1,
indirectly. E1.e refers to the original e3, and
e3.e still refers to the original e2.
Therefore, no garbage collection is possible
until all three objects are set to null, and they
}
}
become an island of references ready for GC.
|
Thanks & Regards
Sidharth Pallai
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
That is a question unrelated to the original thread. Please post that as a new thread; asking unrelated questions is called hi-jacking and can deprive the original poster of contact with their thread.
|
 |
salvin francis
Ranch Hand
Joined: Jan 12, 2009
Posts: 915
|
|
I agree with campbell on this:
Campbell Ritchie wrote:The whole idea of using a high-level language is to avoid having to bother about such questions.
However if you do want such functionality .....
I suggest you use a static variable that is incremented over every Object creation.
|
My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1612
|
|
I just had a question about this in my class. I think the cleanest answer is:
My $0.02,
Janeice
|
When you do things right, people won't be sure you've done anything at all.
|
 |
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
|
|
Janeice DelVecchio wrote:I just had a question about this in my class. I think the cleanest answer is:
Is it thread safe?
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1612
|
|
I know what thread safe means, but I don't know how to tell if something IS thread safe.............
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
Java Concurrency in Action, Janeice, by Brian Goetz and several other people. It's reviewed on our book reviews pages.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
And I think you have the ++ operator in the wrong place, Janeice. And surely the second line belongs inside a constructor??
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1612
|
|
Campbell Ritchie wrote:And I think you have the ++ operator in the wrong place, Janeice. And surely the second line belongs inside a constructor??
The ++ operator is in the correct place. I stand by that. Otherwise, the very first instance would be 0, not 1.
Although I suppose the second line could go in a constructor, if you are only using the default constructor, is it necessary?
I'll check out that book, too.
-Janeice
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1612
|
|
So my guess is that it would be threadsafe if the two lines were inside a "synchronized" constructor:
Better? I'm not really sure about this....
I think the problem with my class is the bar is set pretty low. They care that things compile and work correctly. No one talks about more advanced things. With some of the responses I've gotten from the graders, I bet not many people put much effort into their projects.
|
 |
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
|
|
Janeice DelVecchio wrote:Better? I'm not really sure about this....
This is a threadsafe version of the class. You can't synchronize a constructor so instead a static creation method is introduced. The actual increment can alternatively be made in the private constructor. Many would prefer that for reasons of clarity.
Now objects can be created from any thread and you don't risk having two objects with the same number.
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1612
|
|
so when you make a widget, you do this?
?
|
 |
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
|
|
Janeice DelVecchio wrote:so when you make a widget, you do this?
?
No you use it like an ordinary static method like this,
Widget foo = Widget.next();
Note that a creation method usually is called "create" or has "create" in the name, so maybe "createNext" would've been a better choise.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
. . . or "getInstance()"
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
Campbell Ritchie wrote: . . . or "getInstance()"
But "getInstance" suggests you're going to be getting an instance which already exists, as in a singleton class. If you look through the Java API documentation, there's a large number of methods named getInstance, but they are all (I think) static members of their classes. That's the static factory pattern. Methods which genuinely create a new instance tend to be called newInstance, and there's about 50 of those in the Java API. Some of those are static members, too, but quite a few are not.
There aren't any "createInstance" methods in the API, but there's a long list of "createThis" and "createThat". So a method which creates a Widget could be called "createWidget".
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
|
Point taken, Paul C.
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1612
|
|
hmmmm..... this is so close to that Singleton pattern it's scary. I'm sad that I didn't see it sooner.
Really, the only difference is that the static method is not caring whether or not an instance already exists... it just makes one (and in our version counts the instances).
Thanks! I learn so much around here!
|
 |
 |
|
|
subject: How can we count the number of objects created per class using Java1.5 API?
|
|
|