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
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
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
"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.
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
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;
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;
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;