Montag, 7. Juli 2008

Memory Leaks and Annotations

I admit that I was a bit forgetful maintaining my blog. But I just discovered that I actually had my first comment which motivated me beyond any expectation!

This is just a quick one: One thing I really love about Java is its simplicity, especially in terms of memory management. It works great in 99.9% of the time, the remaining 0.1% is a real time killer though.

There is an amount of tools and mechanisms available to view the heap, count objects, and the likes. JMX does not really much in counting objects and Eclipse TPTP is just too "sandboxy".  Either of them really dont help much when the problem is visible, which typically is too late (thousands of out of memory exceptions a minute). It also proved to be very hard to recreate most of the production scenarios in a test environment.

As a Java Developer I try to anticipate the lifecycle of my objects. When I know that I will be instantiating millions of objects, I will try to either use a pattern (Flyweight, Object Pool or the likes) or I will try to make the object as small as possbile (Struct Like Structure,  Masking Bits for multi boolean fields, etc. ). I think any developer does this, so there should be only little effort in telling the VM what I think should happen. I am thinking of an annotation which tells the VM: "OK, expect this object to be instantiated a couple million times."

But why?

1. Smarter Generation Handling
If the garbage collector knows that the object will be instantiated quite often, it might as well create it outside of "Eden" right of way and save some collection CPU cycles later on.

2. Space Detection
If a class is instantiated too often (configurable threshold) and the instances linger around too long, the garbage collector will check if the class is marked as "Bulk Instancable" (either by pre declared package like java.lang or using an annotation). If this is not the case, a warning is issued informing the developerl / administrator that something bad is about to happen

3. Static Style Checks
Using tools like PMD or the likes, eclipse can warn the user that he or she is breaking certain best practices for writing classes with minimal memory requirements.

2. and 3. can be easily done in a seperate Open Source project. Instrumentation and PMD come to mind. The first idea (optimized memory utilization) is beyond my know how. I am sure that memory layout and the likes can help a lot as well, but then again,  I am too spoiled by Java to know much about physical memory management. 

Ia there anything similar out there?



Keine Kommentare: