Re: object byte size

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/28/03


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



Relevant Pages

  • Re: Oh dear...
    ... I got from you a year or two ago work fine on either interface ... to their full capacity and can be formatted OK with HForm v2.56 ... My most recent purchase was a new 40G Maxtor at £20 from CPC. ... These are 5400rpm ATA100 and perfectly OK for RPC use. ...
    (comp.sys.acorn.hardware)
  • object byte size
    ... I'm currently implementing a Cache system. ... Limiting its capacity with the number of object makes no sense, ... I thought about serializing the object to an array of byte, ...
    (comp.lang.java)
  • Re: Files converted to txt files
    ... interface. ... Mark L. Ferguson ... partitioned hard drive and the C drive filled up to near capacity. ... been trying to take off or move programs, but none of the utilities work ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: New Hard Drive, XP Install is looping
    ... > floppies won't give you anything extra. ... > new drive (interface, capacity, did you create the partition during ...
    (microsoft.public.windowsxp.general)
  • Re: New Hard Drive, XP Install is looping
    ... > floppies won't give you anything extra. ... > new drive (interface, capacity, did you create the partition during ...
    (microsoft.public.windowsxp.general)