#86 Lazy creation of ChemObject fields reduces memory overhead



I am working on an applet that includes a JTable that
has a column displaying 2D structure diagrams (I use
CDK to render the structures). It all works very
nicely until the user tries to load a few thousand
compounds into the applet - the result is an out of
memory exception.

I did some memory profiling with JMP
<http://www.khelekore.org/jmp/> and jProfiler
and discoverd that the ChemObject class initializes
many empty fields that are seldom/never used but that
consume considerable amounts of memory (as they are
Vectors, Hashtables or Arrays).

I modified ChemObject to initialize these fields to
null and delay creation of them until they are needed
(lazy creation). This significantly reduced memory
consumption: 3214 compounds went from consuming 156M
down to 39M after implementing lazy creation.

Attached is the modified ChemObject.java (&
CDKConstants.java). I also reduced the initial sizes
of the pointers and flags arrays and made the fields
private (even subclasses should use the getter/setter
methods rather than access fields directly).

The unit tests all pass.



  • Chris Pudney

    Chris Pudney - 2003-08-14

    Lazy creation mods to ChemObject.java & CDKConstants.java

  • Christoph Steinbeck

    Logged In: YES

    The behavior described here could almost be considered a
    bug. We should check if this is still the case in the
    current version of CDK

  • Egon Willighagen

    Logged In: YES

    I agree, this should be considered a bug. A bad design bug,
    or something...

  • Egon Willighagen

    Logged In: YES

    Put as patch in CVS. Try ant -Ddo.lazy.creation=yes clean


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks