Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix garbage collection for the SpatioTemporalIndexImpRebuilderThread #14

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@
*/
public class SpatioTemporalIndexImpRebuilderThread extends Thread
{
private final WeakReference< SpatioTemporalIndexImp< ?, ? > > index;
private final WeakReference< SpatioTemporalIndexImp< ?, ? > > weakReference;

private final int modCountThreshold;

private final long timeout;

private final boolean rebuildAll;

private SpatioTemporalIndexImp< ?, ? > index;

/**
* Create a new spatio-temporal index rebuilder thread. In its {@code run()}
* method, the thread periodically tries to rebuild spatial indices one by
Expand Down Expand Up @@ -78,7 +80,8 @@ public SpatioTemporalIndexImpRebuilderThread(
{
super( name );
setDaemon( true );
this.index = new WeakReference<>( index );
this.index = index;
this.weakReference = new WeakReference<>( index );
this.modCountThreshold = modCountThreshold;
this.timeout = timeout;
this.rebuildAll = rebuildAll;
Expand All @@ -87,26 +90,27 @@ public SpatioTemporalIndexImpRebuilderThread(
@Override
public void run()
{
while ( !isInterrupted() )
while ( index != null && !isInterrupted() )
{
final SpatioTemporalIndexImp< ?, ? > i = index.get();
if ( i == null )
break;

while ( i.rebuildAny( modCountThreshold ) && rebuildAll )
while ( index.rebuildAny( modCountThreshold ) && rebuildAll )
{}

synchronized ( this )
// NB: The strong reference to the index must be cleared before waiting.
// Otherwise, the index would never be garbage-collected.
index = null;

try
{
try
{
wait( timeout );
}
catch ( final InterruptedException e )
{
break;
}
Thread.sleep( timeout );
}
catch ( final InterruptedException e )
{
break;
}

// Restore the strong reference to the index. It will still be null if
// the spatio-temporal index was garbage-collected during waiting.
index = weakReference.get();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*-
* #%L
* Mastodon Graphs
* %%
* Copyright (C) 2015 - 2022 Tobias Pietzsch, Jean-Yves Tinevez
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.mastodon.spatial;

import org.junit.Test;
import org.mastodon.graph.TestSimpleSpatialEdge;
import org.mastodon.graph.TestSimpleSpatialGraph;
import org.mastodon.graph.TestSimpleSpatialVertex;

/**
* Tests {@link SpatioTemporalIndexImpRebuilderThread}.
*/
public class SpatioTemporalIndexImpRebuilderThreadTest
{

/**
* Test that the {@link SpatioTemporalIndexImpRebuilderThread} allows the
* garbage collection of the {@link SpatioTemporalIndexImp} and the
* associated graph.
*/
@Test
public void testGarbageCollection()
{
// NB: This test creates many "largeObject"s. The test fails with
// an OutOfMemoryException if the objects can not be garbage collected.

long totalMemory = Runtime.getRuntime().totalMemory();
int largeObjectSize = (int) Math.min( Integer.MAX_VALUE - 20, totalMemory / 4 );

// How many large objects do we need to cause an OutOfMemoryException?
long n = 2 * totalMemory / largeObjectSize;

for ( int i = 0; i < n; i++ )
{
TestSimpleSpatialGraph graph = new TestSimpleSpatialGraph() {
private final Object largeObject = new byte[ largeObjectSize ];
};
SpatioTemporalIndexImp< TestSimpleSpatialVertex, TestSimpleSpatialEdge > stIndex = new SpatioTemporalIndexImp<>( graph, graph.getVertexPool() );
new SpatioTemporalIndexImpRebuilderThread( "spatial-temporal-index-rebuild-thread", stIndex, 100, 1000, true ).start();
}
}
}
Loading