[vtkusers] VTK/Java crashes -- WeakGlobalRef or garbage collection problem?

Steve M. Robbins steve at sumost.ca
Mon Jun 18 14:04:26 EDT 2007


On Mon, Jun 18, 2007 at 01:44:27PM -0400, David E DeMarle wrote:
> Hi Eran,
> 
> About a month ago I removed the use of WeakGlobalReferences in
> vtkJavaUtil.cxx to address just this issue. The change has not yet been
> propagated into the vtk 5.X release.
> 
> That solved the problem of the indeterminant crashes you've seen but using
> GlobalReferences means that java wrapped vtk objects in the application code
> will never be destructed by the garbage collecting thread. So in order to
> avoid unbounded memory conumption you have to do the very un-java like thing
> of making sure your references to VTK objects are deleted when you are
> finished with them.
> 
> for example
> int i = 0;
> while (true)
>  {
>   vtkSphereSource sphere = new vtkSphereSource();
>   sphere.SetCenter(i,i,i);
>   i++;
>  }
> 
> This code will eventually consume all available memory.
> Whereas this code will not:
> int i = 0;
> while (true)
>  {
>   vtkSphereSource sphere = new vtkSphereSource();
>   sphere.SetCenter(i,i,i);
>   i++;
>   sphere = null; //causes the destructor to be called.
>  }

That's not correct: nulling a reference doesn't cause the java
finalizer to be called.  The vtkSphereSource object is eligible for
garbage collection at the end of the while-body regardless of whether
you null the ref or not.  The two programs have identical semantics in
that regard.

See http://www.ibm.com/developerworks/library/j-jtp01274.html and read
carefully between the lines the section entitled "Explicit Nulling".

-Steve

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20070618/1a39d556/attachment.pgp>


More information about the vtkusers mailing list