File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes object Memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object Memory" Watch "object Memory" New topic
Author

object Memory

Prash Singh
Ranch Hand

Joined: Aug 23, 2009
Posts: 103

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

Joined: Oct 02, 2003
Posts: 11308
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Prash Singh
Ranch Hand

Joined: Aug 23, 2009
Posts: 103

Dear Sir,

It's not because of need, just for curiosity...
Deepak Nayak
Greenhorn

Joined: Nov 05, 2009
Posts: 3
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

Joined: Nov 05, 2009
Posts: 3
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

Joined: Jan 23, 2008
Posts: 213
you may try to look for Java Profiler


SCJP 6, SCWCD 5, SCBCD 5
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
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

Joined: Aug 23, 2009
Posts: 103

Thanksssssssssssss to all...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object Memory