• Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory Consumption by Objects  RSS feed

 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wanted to confirm,... does the amount of memory consumed by objects of same class is SAME??
or, does it depends upon the number of different functions/variables being called using those objets??
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer may depend on your definition of "consumed". I may have an object that has a reference to a collection. That collection may hold one object, or it may hold 1,000,000. Do you count the memory needed by each of those objects in the collection as what your object "consumes"? You could destroy your object and have those million objects stick around, or they may be freed.

I would say this isn't a straightforward question without better definitions.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote:Just wanted to confirm,... does the amount of memory consumed by objects of same class is SAME??
or, does it depends upon the number of different functions/variables being called using those objets??


Hint: You have two strings. One is holding "hello world". The other is holding the complete works of William Shakespeare. Do you think that they would take the same amount of memory?

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you look for things like the BCEL manual. It is a little dated, but probably still useful.
I think memory footprint may be implementation dependent. Some objects vary in size, particularly arrays. Many classes hide arrays away inside themselves, e.g. String, ArrayList, HashMap. All String objects have the same memory footprint (I think), but they have a pointer to an array of chars, whose size depends largely on how many characters there are in the String.
Otherwise, objects of the same class are the same size, but they may have fields which point directly or indirectly to objects whose size does vary from instance to instance.

I didn't explain that very well.
Fortunately, you can program without knowing any of those things.

Methods do not live in objects. There are Class<Foo> objects as well, which the classloader creates when the Foo class is first needed. As far as I can remember, all methods live inside the Class<Foo> object. So do static fields. If those fields point to objects, objects of the same class are the same size, but they may have fields (as above)…
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of an object as a tree*. The object that you hold has objects within it, and those objects will have other objects, and so on. If you draw it all out, it will look like a tree. Your object is the trunk of the tree. You can think of the objects that hold other objects as the branches, and you can think of the primitive types as leaves in the tree. What matters is the size of the tree, not the size of the trunk. The total size of the tree is dependent on number of branches, number of leaves, and the cumulative size of the leaves. SImilarily, the memory footprint of the object is dependent on the memory footprint of it's children.


*Actually, in real life, you have objects that are shared between objects. SO, it's never as neat as a tree. You need to think of those shared objects as something outside the trees. If you project is well designed, you won't have too many references between trees. Depending on how you designed it, the whole structure might be neat and look like Christmas lights strung through a grove of trees, or a bunch of trees tied to a single maypole. Or it might be messy and look like couple of creepers intertwined with each other
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uhmm.. i'm still not cleared.
okay, lets have a Code..


// Now, will obj1 and obj2 hold same amount of memeory??
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this particular example, yes. Nothing you do in the code causes the memory consumption to be different. In particular, nothing in methods foo1() or foo2() affects the method consumption by the objects obj1 and obj2. You're using local variables there, which are not stored with instances on the heap; rather, they are stored on the stack, and the memory is freed when the method completes. Further, the two String literals are not stored with instances either; they are stored elsewhere in the string pool.

The full answer to your question is really quite complicated, and we don't necessarily know all the details. Different Java implementations may give different results. In general you shouldn't need to know exactly how much memory everything takes. Why do you feel you need this?
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:In this particular example, yes. Nothing you do in the code causes the memory consumption to be different. In particular, nothing in methods foo1() or foo2() affects the method consumption by the objects obj1 and obj2. You're using local variables there, which are not stored with instances on the heap; rather, they are stored on the stack, and the memory is freed when the method completes. Further, the two String literals are not stored with instances either; they are stored elsewhere in the string pool.

The full answer to your question is really quite complicated, and we don't necessarily know all the details. Different Java implementations may give different results. In general you shouldn't need to know exactly how much memory everything takes. Why do you feel you need this?

thanks. Now am cleared... upto an extent.
and i feel like knowing about it, since i just started learning Java .. after JavaScript. In JavaScript i was not required to worry about all this memeory consumption, but in Java, i think the case is completely diffferenr... i guess
:\
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote:t in Java, i think the case is completely diffferenr... i guess

actually, it isn't any different. You really don't need to worry about this stuff. most computers (even handheld devices) have GIGABYTES of memory. Does it matter if an object uses 200 bytes or 250?
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Andy Richard wrote:t in Java, i think the case is completely diffferenr... i guess

actually, it isn't any different. You really don't need to worry about this stuff. most computers (even handheld devices) have GIGABYTES of memory. Does it matter if an object uses 200 bytes or 250?

well, it really might not matter.. but i'm interested in knowing the inner- side of JAVA ;)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Richard wrote:well, it really might not matter.. but i'm interested in knowing the inner- side of JAVA ;)

That's all well and good, but as Mike pointed out...the folks who write the compilers (and JVMs?) are given a lot of freedom. For example, a Boolean only has to provide the FUNCTIONALITY of having two values. It can be implemented however someone wants. One person may write it so it only uses a bit. Another may implement it so it uses a byte. Someone else may write one that uses four bytes.

Someone may write it so the first one uses four bytes, but the 2nd through 32nd don't take any more memory - they piggy back off the first one.

Even if you find out the answer for one compiler/jvm, the answer very well could be different for another.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to be worried about memory consumption when you are loading objects that use lot of memory. It doesn't matter whether it's Java or Javascript. The only thing is if you are using Javascript to provide UI interaction, you are less likely to load heavyweight objects into memory, whereas if you are writing something in Java to process millions of records, you will run into the problems if you didn't do it right. You will have the same problem in Javascript too.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Jayesh Exactly.. but you get more relaxability in case of JavaScript, as far as memory consumption is considered ..... no?
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the language itself doesn't give you anything that make memory usage easier. If you do Javascript on the server side, you will run into the same problems. Actually, a lot of serve side Javascripting solutions like Rhino and Alfresco use Java underneath.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!