Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object Memory

 
Prash Singh
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I was just wondering, is it possible to know the size of the memory occupied by an object?
If so, then how???

 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a more important question is why do you care?

The short answer is that it's really hard, because you shouldn't care. It can vary from JVM to JVM even, so there's no hard, fast rule.
 
Prash Singh
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir,

It's not because of need, just for curiosity...
 
Deepak Nayak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When java was developed first there was no real way to measure the size of an object. Java does not provide any sizeOf() operator like C. This is because in C programmers have to manually allocate memory using malloc().In java, memory allocation and object construction are both tied together, This makes it more difficult to find the size of an object.

Traditional method used is to measure the memory used by the program before creating the object and after creating the object and then checking the difference. The difference will give us the size of memory allocated for that object unless -
(1) The object is created first time, resulting of the loading of the Class for that object in memory which will be in many cases much bigger than the object itself
(2) Garbage collector kicked in after we measured the used memory before creating the object but before measuring the memory used after creating the object. In this case the size recorded could be negative.

According to my knowledge- Yes you can specify the maximum size of an object in memory. maximum size of the object is constructed by its instance variables.
 
Deepak Nayak
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Nayak wrote:When java was developed first there was no real way to measure the size of an object. Java does not provide any sizeOf() operator like C. This is because in C programmers have to manually allocate memory using malloc().In java, memory allocation and object construction are both tied together, This makes it more difficult to find the size of an object.

Traditional method used is to measure the memory used by the program before creating the object and after creating the object and then checking the difference. The difference will give us the size of memory allocated for that object unless -
(1) The object is created first time, resulting of the loading of the Class for that object in memory which will be in many cases much bigger than the object itself
(2) Garbage collector kicked in after we measured the used memory before creating the object but before measuring the memory used after creating the object. In this case the size recorded could be negative.

According to my knowledge- Yes you can specify the maximum size of an object in memory. maximum size of the object is constructed by its instance variables.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may try to look for Java Profiler
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prash Singh wrote:Dear all,
I was just wondering, is it possible to know the size of the memory occupied by an object?
If so, then how???


You can estimate it.

An object has about 16 bytes of overhead. In addition comes the variables. You add 8 bytes for each long and double, 4 bytes for each int, float and reference, 2 bytes for each short and char, and 1 byte for each byte and boolean.

That gives you an approximate size. For example an Integer takes 16+4 = 20 bytes.

 
Prash Singh
Ranch Hand
Posts: 103
jQuery Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanksssssssssssss to all...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic