Hi,
I have to write a cache in java, and I'm looking for a mechanism that
make possible to create memory pools. For example, I set for my cache
20MB limit, and I put objects to this segment of memory and when this
limit will be exceeded JVM will throw exception or return message.
Thanks in advance,
Have a good day
Alexandre Jaquet - 22 May 2007 14:45 GMT
> Hi,
>
[quoted text clipped - 5 lines]
> Thanks in advance,
> Have a good day
Hi,
You can for example define a constat of MAX_SIZE in your cache manager
and then before putting in an hashtable (the cache structure) you can
control the size of the content you put.
My two cents, hope this help.
Regards,
Alexandre
Robert Klemme - 22 May 2007 14:51 GMT
> I have to write a cache in java, and I'm looking for a mechanism that
> make possible to create memory pools. For example, I set for my cache
> 20MB limit, and I put objects to this segment of memory and when this
> limit will be exceeded JVM will throw exception or return message.
That's not a good approach because memory usage is not easy to measure
(also the same object may need a different amount of memory depending on
VM). I'd rather go with Alexandre's suggestion to count objects.
There is a ton of resources on the web about caching implementations and
algorithms for selecting which objects should be removed etc.
robert
Dlugi - 22 May 2007 15:20 GMT
> That's not a good approach because memory usage is not easy to measure
> (also the same object may need a different amount of memory depending on
[quoted text clipped - 4 lines]
>
> robert
Unfortunately, I need api for measuring memory usage of varied object
(this is lists of objects). Runtime.totalMemory and Runtime.freeMemory
are unsufficient, because I get object as a parameter of method.
As a last resort I will use counting this objects, but I hope that there
is api that copes with it.
Thanks very much!!
Robert Klemme - 22 May 2007 15:44 GMT
>> That's not a good approach because memory usage is not easy to measure
>> (also the same object may need a different amount of memory depending
[quoted text clipped - 10 lines]
> As a last resort I will use counting this objects, but I hope that there
> is api that copes with it.
There is a management API which might provide this kind of information.
Be warned though that it is not intended for usage from within the JVM
and likely causes significant overhead.
Why do you "need" memory usage?
robert
B - 22 May 2007 16:17 GMT
> Hi,
>
> I have to write a cache in java, and I'm looking for a mechanism that
> make possible to create memory pools. For example, I set for my cache
> 20MB limit, and I put objects to this segment of memory and when this
> limit will be exceeded JVM will throw exception or return message.
We've used this in the past, no idea if it is what you need:
http://ehcache.sourceforge.net/
Dlugi - 22 May 2007 18:36 GMT
> We've used this in the past, no idea if it is what you need:
>
> http://ehcache.sourceforge.net/
Thanks a lot!!
I know this standard (JCache) but that implementation take 1 second per
1MB when evaluate how many size entries occupy in the memory :/
Piotr Kobzda - 23 May 2007 18:48 GMT
> I know this standard (JCache) but that implementation take 1 second per
> 1MB when evaluate how many size entries occupy in the memory :/
Hmm, maybe instrumentation could help?
Simple example on how to compute approximated object size is there:
http://tinyurl.com/hfzzu
(notice the correction from a post next to it)
Accuracy of that method is much better than with serialization, and
efficiency is not so bad I think.
For example, on my machine ArrayList filled with 1000000 new Objects
consumes about 14MB of memory, and computation of its size takes about
1.5 sec.
piotr
prasanna - 22 May 2007 19:22 GMT
There is another approach where you can serialize an object and
calculate its size. It does not guarantee the exact size but can get
you closer.
-Prasanna
Dlugi - 23 May 2007 12:14 GMT
> There is another approach where you can serialize an object and
> calculate its size. It does not guarantee the exact size but can get
> you closer.
>
> -Prasanna
It's good idea but cause large overhead :/
Bent C Dalager - 23 May 2007 12:41 GMT
>There is another approach where you can serialize an object and
>calculate its size. It does not guarantee the exact size but can get
>you closer.
My recent experience with serialization is that what was 700 bytes
serialized using the default implementation took only 50 bytes or so
after I wrote my own custom serialization for the same objects. (This
matters to me because I want to fit it into one single UDP packet and
about 500 bytes seems to be the suggested maximum payload.)
What I left out, I expect, was administrative data for the objects
contained within the topmost object (and most of the administrative
data for the topmost object itself). My custom method would just write
the floats, ints, etc., contained in each nested object to an
ObjectOutputStream rather than write the actual objects themselves.
I expect my custom serialization is a more accurate estimate of the
in-memory cost of these objects than what the default serialization
was. The accuracy of the approach you suggest will therefore depend
entirely upon how the data objects in question are structured: if
there is much administrative data to be serialized, your estimate
could easily be off by a factor 10.
Cheers
Bent D

Signature
Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
powered by emacs