How can I find the number of live objects on the heap in Java program?
The simplest way is to use jmap tool. If you will print objects histogram at the end you'll see total number of instances and also accumulated size of all objects:
jmap -histo will print whole objects with number of instances and size. The last line will contain total number
Total 2802946 174459656
Second column is total instances count, and last is total bytes.