• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Number of objects?

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it possible in Java to use come command or function to find how many objects or instances were created when a program was running?

Thanks in advance
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are profilers that can be used for this purpose -- its normally done when you're trying to optimize your code (Object creation is a very expensive process). Check out something like Sitraka's JProbe.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Not that I know of. You can create your own class ReferenceNew {} which wraps and returns you an actual. Within this ReferenceNew class, you can maintain a count of objects you created with new. It's easy if you want to keep a count of objects allocated since your program started, but it gets more complicated if you want to keep an accurate count of what objects are still active and the number objects that have been collected. Then you'll have to do some fancy stuff with the finalize() method, which I don't recommend doing unless you really need it.
You can look at the various profilers for Java. They do a very good job of keeping reference counts.
-Peter
 
The knights of nee want a shrubbery. And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic