xml-xmlbeans-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Maeda" <cma...@granitehealth.com>
Subject RE: Finalizers
Date Thu, 02 Oct 2003 14:13:54 GMT
Have you looked at using weak references for the XML->Cursor references?

-----Original Message-----
From: Eric Vasilik [mailto:ericvas@bea.com] 
Sent: Wednesday, October 01, 2003 1:14 PM
To: xmlbeans-dev@xml.apache.org
Subject: RE: Finalizers


My plans for the next version of the store are to remove finalizing
XmlCursor.  Finalizing them in V1, was indeed, a "last-ditch clean-up
effort".    

Let me describe a bit why this situation came about.

The V1 XML store creates fewer objects than the XmlCursor can address.  When
XML content is manipulated, say, moved from one place to another, cursors
within that content need to be moved with it.  This requires the store to
search for these cursors which requires that the XML have a list of cursors
in the vicinity of the XML being manipulated.  Basically, the cursors point
to the XML and the XML points back to the cursors. (note: cursors are
actually implemented with two objects)

This means that if cursors are not closed (we have a dispose method on
cursor which performs this), then the XML will continue to point to the
cursors, not allowing them to be garbage collected.

If users creates and forget cursors many times on the same XML document
without disposing them, you can get into a situation where you run out of
memory.  Cursors do not seem like a resource (like sockets or database
connections), and users can easily not assume they do not need to be
disposed (closed).

Thus, we added a finalizer.  If the user called dispose, the finalizer would
do nothing.

Ironically, recently I looked into a bug where I discovered that the rate of
the creation of cursors (which were properly being disposed) was greater
than the rate that they were being finalized.  This, effective, starvation
caused an out of memory exception!

I've learned my lesson, finalizers bad.  What I would have liked to have
seen was a way for an object to unregister itself for finalization.  With
this, when a cursor was disposed, I could have notified the VM that a
particular object no longer needed finalization.  Does such a thing exist?

- Eric

-----Original Message-----
From: Darrell Teague [mailto:darrell.teague@mantech.com]
Sent: Wednesday, October 01, 2003 8:49 AM
To: xmlbeans-dev@xml.apache.org
Subject: Re: Finalizers


Two more cents on this topic...

Joshua Bloch of Sun and many others (myself included) have argued that
reliance on finalizers for anything other than a last-ditch clean-up effort
is not recommended.  Instead, it is recommended that any resource consuming
classes (i.e., that open socket, file, database connection, whatever)
provide some type of "close" method that should (in practice) be called by
the client.  Granted, there is no way to ensure the client will call such a
method but the finalize() method is not guaranteed to happen, in the
sequence expected, etc either.  At least with a declared public method,
clients can be guaranteed a tidy clean-up process (if they call it) whereas
reliance on finalizers may be non-deterministic since the JVM implementation
and other factors beyond the developers control determine the sequence,
behavior, priority, etc of the call to finalize(). 

>>> don@bea.com 09/30/03 01:12PM >>>
As you may already know, finalizers must be used with extreme caution.  
Because they are run by in the GC thread (when all other threads are 
paused) if a finalizer deadlocks, the entire VM will be frozen.  This 
makes using synchronization in a finalizer particularly dangerous.  
There are also problems around any operations that allocate memory in a 
finalizer.

(Note: my information may be out of date).

    -Don

David Bau wrote:

>Eric, was thinking about the threading/object creation etc issues over the
>weekend.
>
>Another interesting issue: currently we use a finalizer on cursors, but
>finalizers seem to be fairly expensive, so apps that spew out lots and lots
>of cursors have issues.  I wonder what problems we'd have to deal with in
>order to eliminate the finalizer, and whether or not that would be
possible,
>or if it would come into conflict with some of the other parameters of the
>problem just like synchronization?
>
>David
>
>
>- ---------------------------------------------------------------------
>To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
>For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
>Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/
>
>
>
>  
>


- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/

- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/






- ---------------------------------------------------------------------
To unsubscribe, e-mail:   xmlbeans-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-dev-help@xml.apache.org
Apache XMLBeans Project -- URL: http://xml.apache.org/xmlbeans/


Mime
View raw message