Skip to content

Commit

Permalink
perf: better memory cleanup
Browse files Browse the repository at this point in the history
* seems to address the worst offenders
* some of this cleanup is likely overkill

see #134
  • Loading branch information
bogovicj committed Apr 4, 2024
1 parent d38bc18 commit 41f42ca
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 85 deletions.
14 changes: 13 additions & 1 deletion src/main/java/bdv/gui/BigWarpViewerFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public BigWarpViewerFrame(
}

public BigWarpViewerFrame(
BigWarp<?> bw,
final BigWarp<?> bw,
final int width, final int height,
final List< SourceAndConverter< ? > > sources,
final List< ConverterSetup > converterSetups,
Expand Down Expand Up @@ -256,4 +256,16 @@ public Behaviours getTransformBehaviours()
return transformBehaviours;
}

@Override
public void dispose()
{
super.dispose();
keybindings.removeActionMap("bw");
keybindings.removeInputMap("bw");
keybindings.removeActionMap("bwv");
keybindings.removeInputMap("bwv");
keybindings.removeActionMap("navigation");
keybindings.removeInputMap("navigation");
}

}
11 changes: 7 additions & 4 deletions src/main/java/bdv/gui/LandmarkKeyboardProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
import java.awt.KeyEventPostProcessor;
import java.awt.event.KeyEvent;

import net.imglib2.RandomAccessibleInterval;
import net.imglib2.realtransform.AffineTransform3D;
import bigwarp.BigWarp;

public class LandmarkKeyboardProcessor implements KeyEventPostProcessor
{
BigWarp bw;
BigWarp<?> bw;

public LandmarkKeyboardProcessor( BigWarp bw )
public LandmarkKeyboardProcessor( BigWarp<?> bw )
{
this.bw = bw;
}
Expand All @@ -53,4 +51,9 @@ public boolean postProcessKeyEvent(KeyEvent ke ) {
return false;
}

public void dispose() {

bw = null;
}

}
3 changes: 1 addition & 2 deletions src/main/java/bdv/viewer/BigWarpOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public void paint( final Graphics2D g )
textBoxColor = Color.BLACK;
textBoxColorHl = new Color( color.getRed(), color.getGreen(), color.getBlue(), 255 );
textBoxColor = new Color( color.getRed(), color.getGreen(), color.getBlue(), 128 );

}

final int nRows = landmarkModel.getRowCount();
Expand All @@ -161,7 +160,7 @@ public void paint( final Graphics2D g )
// otherwise, take the fixed point
boolean copySuccess = false;
if( isMoving ) {
if( viewer.isInFixedImageSpace() ) {
if( !isMoving ) {
if( landmarkModel.isWarped(index))
copySuccess = landmarkModel.copyWarpedPointSafe(spot, index);
else
Expand Down
70 changes: 3 additions & 67 deletions src/main/java/bdv/viewer/BigWarpViewerPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@

import bdv.cache.CacheControl;
import bdv.gui.BigWarpViewerOptions;
import bdv.img.WarpedSource;
import bdv.util.Affine3DHelpers;
import bdv.util.Prefs;
import bdv.viewer.animate.RotationAnimator;
import bdv.viewer.animate.SimilarityTransformAnimator3D;
import bdv.viewer.overlay.BigWarpMaskSphereOverlay;
import bigwarp.BigWarp;
import bigwarp.BigWarpData;
import bigwarp.source.SourceInfo;
import bigwarp.util.Rotation2DHelpers;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.realtransform.Translation3D;

Expand All @@ -49,8 +46,6 @@ public class BigWarpViewerPanel extends ViewerPanel

public static final int TARGET_GROUP_INDEX = 1;

protected final BigWarpData<?> bwData;

protected BigWarpViewerSettings viewerSettings;

protected BigWarpOverlay overlay;
Expand Down Expand Up @@ -89,7 +84,6 @@ public BigWarpViewerPanel( final BigWarpData bwData, final BigWarpViewerSettings
{
// TODO compiler complains if the first argument is 'final BigWarpData<?> bwData'
super( bwData.sources, 1, cache, optional.getViewerOptions( isMoving ) );
this.bwData = bwData;
this.viewerSettings = viewerSettings;
this.isMoving = isMoving;
this.updateOnDrag = !isMoving; // update on drag only for the fixed
Expand All @@ -103,8 +97,6 @@ public BigWarpViewerPanel( final BigWarpData bwData, final BigWarpViewerSettings
dragOverlay.paint( ( Graphics2D ) g );
}
} );

//updateGrouping();
}

@Override
Expand All @@ -130,16 +122,6 @@ public void precomputeRotations2d( final AffineTransform3D initialViewTransform
}
}

// public void toggleTextOverlayVisible()
// {
// textOverlayVisible = !textOverlayVisible;
// }
//
// public void toggleBoxOverlayVisible()
// {
// boxOverlayVisible = !boxOverlayVisible;
// }

public void setHoveredIndex( int index )
{
if( index != overlay.getHoveredIndex() )
Expand All @@ -164,59 +146,13 @@ public void setHoveredIndex( int index )
@Deprecated
public int updateGrouping()
{
final SynchronizedViewerState state = state();
synchronized ( state )
{
final List< SourceAndConverter< ? > > moving = new ArrayList<>();
final List< SourceAndConverter< ? > > target = new ArrayList<>();

int idx = 0;
for ( final SourceInfo sourceInfo : bwData.sourceInfos.values() )
{
if (sourceInfo.isMoving()) {
moving.add( state.getSources().get( idx ) );
} else {
target.add( state.getSources().get( idx ) );
}
idx++;
}

state.clearGroups();

final SourceGroup movingGroup = new SourceGroup();
state.addGroup( movingGroup );
state.setGroupName( movingGroup, "moving images" );
state.addSourcesToGroup( moving, movingGroup );

final SourceGroup targetGroup = new SourceGroup();
state.addGroup( targetGroup );
state.setGroupName( targetGroup, "fixed images" );
state.addSourcesToGroup( target, targetGroup );

// make only moving and target image groups active in fused mode
state.setGroupActive( movingGroup, true );
state.setGroupActive( targetGroup, true );

// only turn grouping enabled by default if there are multiple moving or
// target images
if ( moving.size() > 1 || target.size() > 1 )
state.setDisplayMode( state.getDisplayMode().withGrouping( true ) );

state.setCurrentGroup( isMoving ? movingGroup : targetGroup );

// TODO: This does not what the javadoc says. Change javadoc? Return void.
return state.getGroups().size();
}
return -1;
}

@Deprecated
public boolean isInFixedImageSpace()
{
if( bwData.numMovingSources() < 1 )
return true;
else
{
return !isMoving || ( ( WarpedSource< ? > ) ( ( bwData.getMovingSource( 0 )).getSpimSource() ) ).isTransformed();
}
return !isMoving;
}

public boolean doUpdateOnDrag()
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/bigwarp/BigWarp.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -231,10 +230,11 @@ public class BigWarp< T >

protected BigWarpData< T > data;

protected final SetupAssignments setupAssignments;
protected final WarpVisFrame warpVisDialog;
protected SetupAssignments setupAssignments;

protected final HelpDialog helpDialog;
protected WarpVisFrame warpVisDialog;

protected HelpDialog helpDialog;

private final KeymapManager keymapManager;

Expand Down Expand Up @@ -310,6 +310,8 @@ public class BigWarp< T >

protected MaskedSourceEditorMouseListener maskSourceMouseListenerQ;

private LandmarkKeyboardProcessor keyboardProc;

protected BigWarpMessageAnimator message;

protected final Set< KeyEventPostProcessor > keyEventPostProcessorSet = new HashSet< KeyEventPostProcessor >();
Expand Down Expand Up @@ -359,7 +361,7 @@ public class BigWarp< T >

protected FileDialog fileDialog;

final JFileChooser fileChooser;
protected final JFileChooser fileChooser;

protected File autoSaveDirectory;

Expand Down Expand Up @@ -615,7 +617,6 @@ public boolean accept( final File f )

tableActions = new BigWarpActions( inputTriggerConfig, "bw-table" );
BigWarpActions.installTableActions( tableActions, getLandmarkFrame().getKeybindings(), this );
// UnmappedNavigationActions.install( tableActions, options.values.is2D() );

keymap.updateListeners().add( () -> {

Expand Down Expand Up @@ -681,7 +682,6 @@ public boolean accept( final File f )
createMovingTargetGroups();
viewerP.state().setCurrentGroup( mvgGrp );
viewerQ.state().setCurrentGroup( tgtGrp );
// viewerQ.state().changeListeners().add(warpVisDialog.transformGraphPanel);

SwingUtilities.invokeLater( () -> {
viewerFrameP.setVisible( true );
Expand Down Expand Up @@ -1087,8 +1087,10 @@ public void removeKeyEventPostProcessor( final KeyEventPostProcessor ke )

public void closeAll()
{
final ArrayList< KeyEventPostProcessor > ks = new ArrayList< KeyEventPostProcessor >( keyEventPostProcessorSet );
for ( final KeyEventPostProcessor ke : ks )
// This method is probably total overkill, but helps avoid an actual
// memory leak

for ( final KeyEventPostProcessor ke : keyEventPostProcessorSet )
removeKeyEventPostProcessor( ke );

repeatedKeyEventsFixer.remove();
Expand All @@ -1103,6 +1105,21 @@ public void closeAll()
viewerFrameP.dispose();
viewerFrameQ.dispose();
landmarkFrame.dispose();

warpVisDialog.dispose();
warpVisDialog = null;

helpDialog.dispose();
helpDialog = null;

keyboardProc.dispose();
keyboardProc = null;

solverThread.interrupt();
solverThread = null;

data.cleanUp();
data = null;
}

public void setUpdateWarpOnChange( final boolean updateWarpOnPtChange )
Expand Down Expand Up @@ -2674,7 +2691,8 @@ public PlateauSphericalMaskSource getTransformPlateauMaskSource()

public void setupKeyListener()
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor( new LandmarkKeyboardProcessor( this ) );
keyboardProc = new LandmarkKeyboardProcessor( this ) ;
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor( keyboardProc );
}

public void setWarpVisGridType( final GridSource.GRID_TYPE type )
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/bigwarp/BigWarpData.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import net.imglib2.realtransform.InvertibleWrapped2DTransformAs3D;
import net.imglib2.realtransform.RealTransform;
import net.imglib2.realtransform.Wrapped2DTransformAs3D;
import net.imglib2.util.Intervals;

public class BigWarpData< T >
{
Expand Down Expand Up @@ -234,6 +233,14 @@ public void wrapUp()
{
}

public void cleanUp()
{
System.out.println("data clean up");
sources.clear();
sourceInfos.clear();
converterSetups.clear();
}

void addSource( Source<T> src, boolean isMoving )
{
addSource( src, isMoving, null );
Expand Down

0 comments on commit 41f42ca

Please sign in to comment.