Re: object byte size
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/28/03
- Next message: Anthony Borla: "Re: What is the purpose of all these Streams?"
- Previous message: FISH: "Re: What is the purpose of all these Streams?"
- In reply to: Jéjé: "object byte size"
- Next in thread: Jesper Matthiesen: "Re: object byte size"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 28 Nov 2003 11:25:27 GMT
"Jéjé" <jerome.eteve@it-omics.com> wrote in message
news:pan.2003.11.28.11.22.58.476623.24130@it-omics.com...
>
> Hi everyone.
> I'm currently implementing a Cache system.
> Limiting its capacity with the number of object makes no sense,
> since objects can have any size they want.
>
> So I'd like to limit its capacity in term of number of bytes
> already cached. Therefore, I need to know what is the size
> in byte of any object in the heap.
>
> I thought about serializing the object to an array of byte, but
> this method implies that the user is forced to make its objects
> serializable, which is neither practical, nor kind to him or her.
> Furthermore, serializing objects just to know their size in byte
> is not efficient.
>
> Is there a way to do this in a simple and elegant way ?
>
> Thanks for your answers.
>
Unfortunately Java does not implement the equivalent of the C / C++ 'sizeof'
operator, so it is not possible to actually *know* the size of an object.
AFAIK, all you can do is make estimates of object size [e.g. add up the
sizes of all primitive-type fields (since these values are fixed), and
(maybe ?) assume a size for each reference field].
To 'mark' a class as 'cacheable' you could implement an interface, say:
interface Cacheable
{
int size();
}
which, aside from so identifying a class, also returns an estimate of its
size in bytes. If size estimates meet your requirements this might be
viable. Of course you will encounter the same problem that the classes
implementing the 'Cloneable' interface encounter - no means of ensuring each
and every non-primitive field also implements the interface.
I hope this helps.
Anthony Borla
- Next message: Anthony Borla: "Re: What is the purpose of all these Streams?"
- Previous message: FISH: "Re: What is the purpose of all these Streams?"
- In reply to: Jéjé: "object byte size"
- Next in thread: Jesper Matthiesen: "Re: object byte size"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|