By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

object byte size

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Jéjé" <je**********@it-omics.com> wrote in message
news:pa**********************************@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
Jul 17 '05 #2

P: n/a
> 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
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.