Jeremy,

This part looks correct.  My only suggestion to improve the change is to
split the cleanup responsibilities between the classes that define the
data members (and add the fData field as well):

In XMLEntityReader.java:

    protected XMLEntityHandler.EntityReader changeReaders() throws
Exception {
        XMLEntityHandler.EntityReader nextReader = null;
        if (fStillActive) {
            nextReader = fEntityHandler.changeReaders();
            fStillActive = false;
       // Allow these following three fields to be GC-ed.
            fEntityHandler = null;
            fErrorReporter = null;
            fCharDataHandler = null;
        }
        return nextReader;
    }

In StringReader.java:

    public XMLEntityHandler.EntityReader changeReaders() throws Exception {
        XMLEntityHandler.EntityReader nextReader = super.changeReaders();
        synchronized (StringReader.class) {
            fNextFreeReader = fgFreeReaders;
            fgFreeReaders = this;
       // Allow these following two fields to be GC-ed.
            fStringPool = null;
          fData = null;
        }
        return nextReader;
    }

Regards,
Glenn



                                                                                       
                            
                    Jeremy Carroll                                                     
                            
                    <[EMAIL PROTECTED]       To:     [EMAIL PROTECTED]           
                            
                    hp.com>              cc:                                           
                            
                                         Subject:     Fix for memory leak in 
StringReader                          
                    08/03/2001                                                         
                            
                    10:01 PM                                                           
                            
                    Please respond                                                     
                            
                    to                                                                 
                            
                    xerces-j-dev                                                       
                            
                                                                                       
                            
                                                                                       
                            




Snapshot: xml-xerces_20010803161801.tar.gz
package   org.apache.xerces.readers;
file:     StringReader.java

Old code: lines 216 ==> 223

    public XMLEntityHandler.EntityReader changeReaders() throws
Exception {
        XMLEntityHandler.EntityReader nextReader =
super.changeReaders();
        synchronized (StringReader.class) {
            fNextFreeReader = fgFreeReaders;
            fgFreeReaders = this;
        }
        return nextReader;
    }

New code: 5 new lines added, otherwise unchanged.


    public XMLEntityHandler.EntityReader changeReaders() throws
Exception {
        XMLEntityHandler.EntityReader nextReader =
super.changeReaders();
        synchronized (StringReader.class) {
            fNextFreeReader = fgFreeReaders;
            fgFreeReaders = this;
       // Allow these following four fields to be GC-ed.
            fEntityHandler = null;
            fErrorReporter = null;
            fCharDataHandler = null;
            fStringPool = null;
        }
        return nextReader;
    }

Rationale:

particularly after parsing has finished, the static reference to
fgFreeReaders remains, and is not garbage collectable. Hence, if we do
not null out the member variables we also maintain references to them,
and they and all their substructure is not garbage collectable:

Jeremy Carroll
HP Labs
Bristol




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to