• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Memory overhead from class methods?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is thus this: How much additional memory do member methods within a class add to multiple instances of that class. I am not sure if the JVM genuinely creates a new version of each member method for each class or if it really only creates a single "copy" of the member method and then when that member method is run by an instance of the class, uses that specific instances member variables in the copy. (Note: I am not asking what the Java language claims is done, I am asking what the JVM really does behind the scenes).

Motivation for the question (in case I am asking the wrong question):
I am writing a piece of code that will generate pictures of "complicated" mathematical curves, which I am storing in an array. The curves are consturcted iteratively and each iteration causes the number of segments to grow exponentially. Thus (to my limited understanding) the size of the data that represents a segment of the curve is the main limitation on how many iterations the code can go through quickly (i.e. we can't control the number of segments, that is implicit in the problem, but when we are storing so many of them that we run out of memory then the computer has to use the hard drive for memory and substantial slow down takes place). The code should be able to go through as many iterations as quickly as possible. (If we didn't care about speed I expect we could keep the data in a file and load and work with large segments of the data at a time, which is maybe what I should be doing anyway...I don't know if that can be made fast)
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods and their bytecode are associated with the class itself, not with instances of the class. Therefore, having many or complicated methods in a class does not cause an increase in the per-instance memory consumption.

The per-instance memory consumption is a function of the number and type of instance fields.

In Java, code is always part of a class. Other languages (e.g. JavaScript, which is much less like Java than the name suggests) do allow bits of code to be directly owned by objects. But even with such languages, I would not expect them to make a copy of each piece of code defined in the class, when creating a new object. That would just be insanely consumptive of memory.
 
John Robertson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. That is quite helpful.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic