Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to know how many objects are on the heap?

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 51427
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason there is no such technique is that it is something you do not need to know.
 
Maneesh Godbole
Saloon Keeper
Posts: 11331
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have always held that companies which ask such kind of inane questions, are not worth working for.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 51427
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic