Custom memory pool.

Hi!

I have been studying different ways to implement object cache for server application(s). At the server there areXML files which have to be parsed and validated before delivering to clients. Clients are also sendingXML data back to be validated and stored by the server. It would be nice to have object cache between servlets and IO to make server run faster. The ultimate requirement for cache is:There have to be way to set upper limit for caches memory footprint to avoid OutOfMemory.

I found couple of third parties object caches likeJava Caching System (JCS), OSCache, Commons Collections, JCache API but none is supporting cache policy to fulfil my requirement. Am I right? Is there any others? (Open Source)

I started also desining my own implementation of object cache but the problem is:How to calculate object size in bytes.

I tried to solve problem by creating interfaceCacheable that contains methodsizeOf(). It is quite easy to implement and calculate approx size of (simple data) object exceptString. I wrote my on custom String class to get size. I also wrote someCacheable implementations of collection classes too. But next problem was that (object) cache become too specific.

Okey, nowJMX questions : )

1. Question is: Is it possible to useJMX to create totally new memory pools within sameVM for object cache.Why? It would be easy to examine memory usage of cache by examining memory usage of those pools.

2. Is there way to set a classloader to load certain classes to those memory pools.

3. Is it possible to set a "rule" that instances of those classes are placed also into new pools.

4. DoesJMX inspection have some severe performance drawbacks?

Plan B

I have also considered idea of running object cache as independentservice in its ownVM. And connecting to it byRMI. Any comments? Performance issues? Have anybody done this?

Thanks!

[2111 byte] By [drekepa] at [2007-11-26 17:40:21]
# 1

I do not think it is possible for user code to create a new memory pool in the sense of the ones that show up in JConsole's Memory tab ("PS Eden Space", "PS Perm Gen", and so on.)

I would in any case advise against getting into the details of object sizes. The question is very difficult - for example, is the size of an object the total size of all objects reachable from it? If so then if x and y are two objects that have part of their object graph in common, adding the sizes you compute for x and y will produce a size that is too big. If you want to get involved in this anyway, a good starting point is <http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html>.

You can in fact get a number representing the size of a Java object by writing a premain method and then using Instrumentation.getObjectSize. See java.lang.instrument. But this size does not include objects referenced from the given object, so it does not really tell you how much memory the object is taking up. For example, all String objects will show the same size, because the char[] containing the actual contents of the String is another object that is referenced by the String.

The usual way to handle object caching is with SoftReference, see java.lang.ref. Admittedly, this does not give you an easy way to balance the sizes of different pools.

emcmanusa at 2007-7-9 0:08:31 > top of Java-index,Core,Monitoring & Management...