• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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
 
And then the flying monkeys attacked. My only defense was this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic