Cache or memory pools in java

D

Dlugi

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
 
A

Alexandre Jaquet

Dlugi said:
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

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
 
R

Robert Klemme

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
 
D

Dlugi

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

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!!
 
R

Robert Klemme

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.

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

B

Dlugi said:
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/
 
P

prasanna

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
 
D

Dlugi

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 :/
 
B

Bent C Dalager

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
 
P

Piotr Kobzda

Dlugi said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top