[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting size of a class



More thoughts;

There's two parts to an object's size;  behavior and data.
The class loader creates only one instance of the behavior no matter
how many instances there are.  The data is what's unique to each instance.

This makes sense if you are familiar to memory models with separate I & D
spaces.

The .class is a rough bytecode representation of the behavior size.  I
don't know how the VM translates the bytecode into memory instance size??

The data size can't be measured except at run time via some technique.
I.E. hashmaps, vectors grow per your algrorithm...

curt


Vikrant.Verma@alltel.com wrote:
> chris,
> There is no direct function like sizeof in java.
> 
> Though u can compute the size of the class following the JVM specs. To
> compute look at size of each primitive in some java cram book. Each
> object is 8 bytes. I think there is some consideration for inheritance
> but i think that too u can find from some book. add them together you
> will have the size of the object.
> 
> You can create an interface which all classes should implement. Expose a
> method in that interface to return sizeof the object. And then compute
> the sizeof manually using the rules. 
> 
> I think this will be too fragile but will work with experienced
> developers.
> 
> -Vikrant
> -----Original Message-----
> From: Christopher Fowler [mailto:cfowler@outpostsentinel.com]
> Sent: Monday, October 07, 2002 10:08 PM
> To: chsmith@speakeasy.net; cfowler@outpostsentinel.com;
> ajug-members@ajug.org
> Subject: Re: Getting size of a class
> 
> 
> 
> Is the size of the .class file a good example.  It does not tell me 
> much if a variable it not populated but it does tell me the size of an 
> empty class.
> 
> Chris
> 
> 
>>One technique that I've seen, since there is no direct method is to 
>>serialize
>>the object and then check the size of the resultant array, String 
> 
> etc. 
> 
>> I've heard
>>this is not inexpensive.
>>
>>If you do this only for development metrics you might not care about 
>>expense.
>>A simple way since these will be your classes, is to implement 
>>Externalizable and
>>then to put you more efficient writeExternal() method that will just 
>>count the bytes
>>vs really go through the effort of writting to the outputStream.
>>
>>Good luck,  curt
>>
>>Christopher Fowler wrote:
>>
>>
>>>I'm starting a big java project and I want to be very memory usage 
>>>aware.  I found a way to get the size of the heap.  Is there a way I 
>>>can get the size of a class?  Normally in C I can use sizeof() on 
>>>structures.  Is there something similar?
>>>
>>>Thanks,
>>>Chris
>>>
>>> 
>>>
>>
>>-- 
>>
>>Curt Smith
>>chsmith@speakeasy.net
>>(h) 404-294-6686
>>(w) 404-463-0973
>>
>>
>>
>>
>>
>>
>>
> 
> 


-- 

Curt Smith
chsmith@speakeasy.net
(w) 404-463-0973
(h) 404-294-6686