wood burning stoves 2.0*
The moose likes Java in General and the fly likes How to know how many objects are on the heap? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to know how many objects are on the heap?" Watch "How to know how many objects are on the heap?" New topic
Author

How to know how many objects are on the heap?

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

a friend of mine asked ,as it was asked to him in an interview.
How to know , how many objects are on the heap or how many objects at a particular time in Java
I think somethng like as many new that many objects
factory methods that return an instance
objects created through autoboxing

If someone know


Oracle Java Programmer , Oracle SQL Expert
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
The reason there is no such technique is that it is something you do not need to know.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10175
    
    8

I have always held that companies which ask such kind of inane questions, are not worth working for.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

I don't think there is a standard way to know the number of objects on the heap.
Although you can check the used heap size using the Java VisualVM provided with the Java Package.

cheers


http://plainoldjavaobject.blogspot.in
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

There is a hack on how to achieve this, however I extremely denounce the use of this technique ( and if this technique was feasible and plausible ) , it might have already been included in the Java API. The trick is to override the java.lang.Object class. I read about it in this article quite some time ago Link . The feasibility of this technique is , of-course, open for discussion.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Saif Asif wrote:There is a hack on how to achieve this,. . .
And that hack says this
On other VMs it might reformat your harddrive, sell your credit card details or promote you to sales manager.
Promotion to sales manager is of course the worst possibility amongst those outcomes.

He did say it worked on JDK1.3.1; on newer JDKs, however, it is impossible to create classes in packages whose names begin with java, so that trick no longer works.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Campbell Ritchie wrote: And that hack says this
On other VMs it might reformat your harddrive, sell your credit card details or promote you to sales manager.
Promotion to sales manager is of course the worst possibility amongst those outcomes.

heheheheh you got that right I hope a sales manager isn't reading this post

Campbell Ritchie wrote: on newer JDKs, however, it is impossible to create classes in packages whose names begin with java, so that trick no longer works.

I got some question about this thing , I dont understand this actually. You mean we cant create a package with the name of "java.lang" and have a class name as Object ?

Am asking because I never tried it before and I eventually tried making a class ( immediately after reading your reply ) as below and it seemed to compile fine.

Is this what you had mentioned ? I made it in eclipse Juno with JDK1.7 and it didnt give me any warning and error . Whats wrong over here ? :-\ (confused)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Saif Asif wrote:You mean we cant create a package with the name of "java.lang" and have a class name as Object ?

Am asking because I never tried it before and I eventually tried making a class ( immediately after reading your reply ) as below and it seemed to compile fine.

Is this what you had mentioned ? I made it in eclipse Juno with JDK1.7 and it didnt give me any warning and error . Whats wrong over here ? :-\ (confused)


My guess would be that it's not the compiler that stops you, but the class verification step of the classloader.

Some compilers might choose to disallow that (if the JLS doesn't prevent them from doing so), but even if they did, that would not be sufficient to enforce the rule, since anybody could generate the bytecode without using a Java compiler, or could manipulate other bytecode that did make it past the compilation step.

If this rule is in fact enforced, it would have to be at runtime, by the JVM.

Did you try actually running code that uses this Object class?

Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Jeff Verdegan wrote:
Saif Asif wrote: I made it in eclipse Juno with JDK1.7 and it didnt give me any warning and error . Whats wrong over here ? :-\ (confused)

My guess would be that it's not the compiler that stops you, but the class verification step of the classloader.

Well when I got this evil idea of writing my own version of the Object class, I simply killed the compiler
(With a evil laugh) Reported and evaluated as a bug for JDK 1.6.0 Looks like they fixed it in a later release although the status is unresolved.


SCJP, SCWCD.
|Asking Good Questions|
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Jeff Verdegan wrote:
Saif Asif wrote:You mean we cant create a package with the name of "java.lang" and have a class name as Object ?

Am asking because I never tried it before and I eventually tried making a class ( immediately after reading your reply ) as below and it seemed to compile fine.

Is this what you had mentioned ? I made it in eclipse Juno with JDK1.7 and it didnt give me any warning and error . Whats wrong over here ? :-\ (confused)


My guess would be that it's not the compiler that stops you, but the class verification step of the classloader.

Some compilers might choose to disallow that (if the JLS doesn't prevent them from doing so), but even if they did, that would not be sufficient to enforce the rule, since anybody could generate the bytecode without using a Java compiler, or could manipulate other bytecode that did make it past the compilation step.

If this rule is in fact enforced, it would have to be at runtime, by the JVM.

Did you try actually running code that uses this Object class?




Well I came across a number of new experiences when trying to attempt to create my very own java.lang.Object class.

Scenario 1 (main method inside the object class)
code snippet I used

If we try to compile and run this code from Eclipse , it seems that Eclipse compiler will always go for the java.lang.Object class of the original JDK configured in the build path , it will NOT use our version of the java.lang.Object like this. ( we can however put our implementation of the java.lang.Object in a jdk and package it configure it to the JDK for the eclipse but I didnt try this yet. ) . To use my version of the java.lang.Object , I compiled and ran the code from command line and it seemed to 'compile' fine although when I tried to run it , it threw the following excpetion



Scenario 2 (main method in different class but in same package java.lang
Code snippets

This again compiled fine but threw the exception ( from inside eclipse and from command line as well )

So its still a no , I could compile it but I couldn't and run it.

As for the bug
Amit wrote: Reported and evaluated as a bug for JDK 1.6.0 Looks like they fixed it in a later release although the status is unresolved.

I think the bug is kept unresolved still due to this runtime exception but its just my assumption since they clearly mention it to be related to javac component and not the java class loader or maybe I might be wrong someplace.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to know how many objects are on the heap?
 
Similar Threads
Visualization of Heap Memory In java
How many objects will be created in Heap when we have multiple instances of a specific object?
how to know the objects created for the class
Out of memory error
garbage collection