• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory management  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to All..I do have a small trecky question..
Suppose u have a class(file1) which contains only methods..say 5 methods with blank implementations of all these methods..take an example of such method--
void method1(){ }
void method2(){ } and so on.....compile this file --(say file1.java)

And on the other hand....
u have a class(file2) which contains only methods..say 10 methods with blank implementations of all these methods.
void method6(){ }
void method7(){ }...so on...compile it (say file2.java)

Now My point is that.....
So if u create objects of file1 and file2, can u tell me how much memory they would going to be allocated...???How this situation is being handled away???

Thnx in advance .....for ur most valuable suggestions...
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Methods contribute absolutely nothing to the size of individual objects. An instance of a class with a thousand methods and no member variables is exactly the same size as an instance of a class with five methods and no member variables. The JVM keeps just a single copy of the code for a class, and all the instances "share" that copy.

Does that answer your question?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prashant,

You can also read book "Inside JVM" and some sample free chapters on the Artima.com site.

Here is the link where you will find the memory allocation in JVM.
http://www.artima.com/insidejvm/ed2/jvm2.html

Here as you will see, there is a "method area" where all the methods are stores for classes regardless of how many objects are there for any class which is what Ernest was pointing to.

More about the book here,
http://www.artima.com/insidejvm/blurb.html

Regards,
Maulin
 
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If things are going out of control, you can use a profiler to look for possible memory mistakes in your classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!