-4

Using static increment in Constructor can give me total objects created, but it does not give me live objects count. so how can i get the live objects at a particular instance?

skiwi
  • 66,971
  • 31
  • 131
  • 216
user3510364
  • 138
  • 3

1 Answers1

0

Decrement the static counter in the finalize() method. Note that this is not surefire, but it should give you a decent estimate.*

*According to the Oracle documentation "The Java programming language does not guarantee which thread will invoke the finalize method for any given object," so the accuracy of your counter will be pretty unpredictable.

Azar
  • 1,086
  • 12
  • 27
  • 2
    Depending on the longevity of the object, the reading could be off by many orders of magnitude. However, performing `System.gc()` several times before taking a reading would greatly improve it. – Marko Topolnik Apr 10 '14 at 20:07
  • @MarkoTopolnik Yeah, I'm suggesting this as more of a quick and easy fix, as opposed to a more sophisticated and accurate approach. In an uncomplicated situation, I should think that the gc will behave a little more predictably and this will yield a good ballpark estimate (but I'm no expert). – Azar Apr 10 '14 at 20:13
  • Actually, in a completely basic situation the reading will typically be very much off for short-lived objects. You could even make a quick test, instantiating 10,000 small objects without holding a reference to any. All 10,000 will still registered as "live" if you don't explicitly call `System.gc()`. – Marko Topolnik Apr 10 '14 at 20:16
  • I just ran it: `for (int i = 0; i < 10_000; i++) new Main(); System.out.println(instanceCount.get());` It printed `9396`. – Marko Topolnik Apr 10 '14 at 20:18
  • @MarkoTopolnik Yeah, since the gc won't feel the need to conserve memory, I suppose. But isn't that a different question, objects that are _eligible_ for gc vs. objects that _have been_ gc'd? – Azar Apr 10 '14 at 20:19
  • You could store a `WeakReference` to each object, and have a `ReferenceQueue` that updates the static count. That would probably get the best results without all the badness of `finalize`. – Louis Wasserman Apr 10 '14 at 20:19
  • @LouisWasserman A `PhantomReference` may be an even better choice; however, none of the techniques will reflect the number of truly live (reachable) objects because that count is essentially expensive to find. – Marko Topolnik Apr 10 '14 at 20:22
  • @LouisWasserman In fact, I believe [this](http://stackoverflow.com/a/3478862/2206044) answer suggests a similar idea. – Azar Apr 10 '14 at 20:25
  • @azar The GC is simply lazy and won't collect anything until it hits an "allocation failure" within Eden Space, whose initial size is many megabytes. – Marko Topolnik Apr 10 '14 at 20:27
  • @MarkoTopolnik Yeah that's what I figured. If I may ask, how did you become so knowledgeable about GC in Java? I've read a good bit of material about the language, but nothing that's covered quite this extensively... – Azar Apr 10 '14 at 20:32
  • Well, mostly hanging out on Stack Overflow :) Also, a great way to gain intuition about the GC is watching your application's guts through VisualGC (a standard plugin in VisualVM). Just five minutes with that can give you more insight than hours spent reading about it :) – Marko Topolnik Apr 10 '14 at 20:34
  • @MarkoTopolnik Yeah, S.O. has taught me more than I can express. Thanks, and I'll check out VisualVM. – Azar Apr 10 '14 at 20:38