wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Memory overhead from class methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Memory overhead from class methods?" Watch "Memory overhead from class methods?" New topic
Author

Memory overhead from class methods?

John Robertson
Greenhorn

Joined: Feb 12, 2007
Posts: 7
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)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
John Robertson
Greenhorn

Joined: Feb 12, 2007
Posts: 7
Thank you. That is quite helpful.
 
GeeCON Prague 2014
 
subject: Memory overhead from class methods?