OVH Cloud OVH Cloud

object byte size

2 réponses
Avatar
Jéjé
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 allready
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.

Jérôme.

--
--
He's just a politician trying to save both his faces...
----------------------------
jerome.eteve_at_it-omics.com

2 réponses

Avatar
Anthony Borla
"Jéjé" wrote in message
news:

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

Avatar
Jesper Matthiesen
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 allready
cached.


OK - I'm not quite sure if this is is something you can use but have a look
at:

public void showMemoryInfo()
{
System.setProperty("java.compiler", "javac");
Runtime runtime = Runtime.getRuntime();
long totalMemory = runtime.totalMemory()/1024;
long freeMemory = runtime.freeMemory()/1024;

System.out.println("Total memory: " + totalMemory + " kilobytes.");
Runtime.getRuntime().gc();
System.out.println("Free memory: " + freeMemory + " kilobytes. Also: ");
System.out.println(Math.round(
(((double)freeMemory)/((double)totalMemory)) * 100.0) + "%)");

}

Regards Jesper