Friday, December 03, 2004

Java Memory Pools

Having developed primarily in garbage collected languages (perl, java, php), I read this article on memory allocation techniques mostly out of curiosity. The most interesting section was on using memory pools instead of the standard calls to malloc() and free(). Apache allocates a memory pool for each request. Any memory needed only for the request is put into this pool. At the end of the request the whole pool is freed.

I've been working on a garbage intensive java application that does lots of EDI and XML translation. Reading XML and mapping objects creates lots of garbage quite quickly and the JVM spends a fair amount of time clearing up garbage. It seems that some sort of pool would help in this case. I would think this could be useful for lots of transactional and web based systems.

How it would work is another matter. Perhaps a "pool context" could be associated with a thread.


MemoryPool pool = Runtime.createMemoryPool();
Thread.currentThread().setMemoryPool();

Foo foo = new Foo(); // allocated in pool
Bar bar = new Bar(); // allocated in pool

The garbage collector would not see foo or bar. It would only see and collect from pool.It's more complicated than that, though, since you could allocate
something in the pool and then assign it to a variable (for example,a Map) outside of the pool.


HashMap map = new HashMap();

MemoryPool pool = Runtime.createMemoryPool();
Thread.currentThread().setMemoryPool();

Foo foo = new Foo(); // allocated in pool
map.put("Foo", foo);


In this case the garbage collector would see foo.

The compexity probably outweighs any real benefits, but I find the idea interesting either way.

1 comment:

Anonymous said...

Hi,

i first used this feature under nextstep / mach. It was called a zone. So an object (in objective-C) created a zone and all its child objects could be allocated from that zone .. and so forth .. and you could free a complete zone.

Take a look at:
http://www.channelu.com/NeXT/NeXTStep/3.3/nd/Concepts/Performance/F_ZoneAllocation.htmld/index.html

that might give you an impression.
It was (maybe it is still in MacOS X) a nice solution