• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Big fat objects

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got fairly high level memory consideration type questions.

If you have a class with thousands of lines of code does this mean when you instantiate the class the associated object is bigger than and object of a relatively lean class?
How does what's inside the class affect it's size? e.g. If the class has no instance variables but lots of methods defined does this necessarily create a large object?
Which will use less memory?


or



P.S. I have no idea how the above will format, why is there no preview button?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is precisely one copy of the code for a loaded class; it's not replicated in each object. The size of an object is unrelated to the number or complexity of its methods. Only the number and size of its members count. So every object with three int members (for example) is going to take up 12 bytes, (3 times 4 bytes per int) plus perhaps some per-object overhead determined by the JVM implementation that's the same for all classes.
 
dave hopkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So are you saying the two implementations of class B will use the same amount of memory?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the second one has a member variable "a" which will take up 4 bytes; the first one does not. Plus, of course, in the second implementation, two objects are created, while in the first, there's just one. Therefore in the second case you have twice the per-object overhead.
[ January 29, 2008: Message edited by: Ernest Friedman-Hill ]
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's only one implementation of class B.

The CLASSes will be different sizes.

The instances of a class will be as big as their (non-static) instance variables (roughly).
 
dave hopkins
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok guys, thanks for the info!
reply
    Bookmark Topic Watch Topic
  • New Topic