[vtkusers] vtkPanel & Memory Consumption

Mark Hünken mark.huenken at web.de
Tue Jan 27 09:53:04 EST 2004


Hi there,

I'm currently implementing a Java application that heavily uses vtkPanel components. Every time a new set of data is selected for viewing panels are removed and new instances (1-4) are created and displayed.

I'm somewhat confused about the amount of memory a vtkPanel consumes and if the resources are properly released, so I did the following sample program based on the SimpleVTK application. Four vtkPanels are instantiated and displayed. By checking/unchecking the check box you can remove panels, respectively create new ones.

To my surprise (well actually based on my work on that other application I expected this), my swap file (I'm using Windows XP) increases by 5 MB per new set of panels. The size of this memory leak is relative to the number of vtkPanels.

So why is this application consuming so much memory and why aren't the resources released? I'm explicitely using VTKDelete() calls to the VTK objects, and I also try to remove the Renderer & RenderWindow themselves.

I cannot rely on Java's Garbage Collection to eventually trigger the removal of graphics resources because the size of the displayed data in the original application is too huge to hold it twice in memory.

Thanks,
Mark


******************************** VtkMemoryTest.java ********************************

package vtkmemorytest;

import vtk.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class VtkMemoryTest implements ItemListener
{

    final int NR_OF_VIEWPORTS = 4;
    JFrame frame;
    JPanel viewportPanel;
    JPanel vis3dPanel;
    JCheckBox vis3dSwitch;
    SimpleVTK[] simplePanel = new SimpleVTK[NR_OF_VIEWPORTS];

    public VtkMemoryTest()
    {
        buildGUI();
    }

    public void buildGUI()
    {
        frame = new JFrame("VTK Memory Test");
        viewportPanel = new JPanel();
        viewportPanel.setSize(600, 600);
        viewportPanel.setLayout(new BorderLayout());
        vis3dPanel = new JPanel();
        vis3dPanel.setSize(500, 500);
        vis3dPanel.setLayout(new FlowLayout());
        vis3dSwitch = new JCheckBox();
        viewportPanel.add(vis3dPanel, BorderLayout.CENTER);
        viewportPanel.add(vis3dSwitch, BorderLayout.SOUTH);
        switch3DOn();
        frame.getContentPane().add(viewportPanel);
        frame.pack();
        frame.setVisible(true);
        vis3dSwitch.addItemListener(this);
    }

    public void switch3DOn()
    {
        vis3dSwitch.setSelected(true);

        for (int idx = 0; idx < NR_OF_VIEWPORTS; idx++)
        {
            simplePanel[idx] = new SimpleVTK();
            simplePanel[idx].setSize(200, 200);
            simplePanel[idx].setVisible(true);
            vis3dPanel.add(simplePanel[idx]);
        }

        vis3dPanel.validate();
    }

    public void switch3DOff()
    {
        vis3dSwitch.setSelected(false);
        vis3dPanel.removeAll();
        vis3dPanel.validate();

        for (int idx = 0; idx < NR_OF_VIEWPORTS; idx++)
        {
            simplePanel[idx].release();
            simplePanel[idx] = null;
        }
    }

    public void itemStateChanged(ItemEvent event)
    {
       if (vis3dSwitch.isSelected())
           switch3DOn();
       else
           switch3DOff();
    }

    public static void main(String[] args)
    {
        VtkMemoryTest vtkMemoryTest1 = new VtkMemoryTest();
    }
}

******************************** VtkMemoryTest.java ********************************

package vtkmemorytest;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import vtk.*;

public class SimpleVTK extends JPanel
{
    vtkPanel renWin;
    vtkPolyDataMapper coneMapper;
    vtkConeSource cone;
    vtkActor coneActor;

    static
    {
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
        try
        {
            System.loadLibrary("vtkHybridJava");
        }
        catch (Throwable e)
        {
            System.out.println("cannot load vtkHybrid, skipping...");
        }
    }

    public SimpleVTK()
    {
        setLayout(new BorderLayout());

        renWin = new vtkPanel();
        cone = new vtkConeSource();

        cone.SetResolution(8);
        coneMapper = new vtkPolyDataMapper();

        coneMapper.SetInput(cone.GetOutput());
        coneActor = new vtkActor();

        coneActor.SetMapper(coneMapper);
        renWin.GetRenderer().AddActor(coneActor);

        add(renWin, BorderLayout.CENTER);
    }

    public void release()
    {
        renWin.GetRenderer().RemoveActor(coneActor);
        cone.VTKDelete();
        cone = null;
        coneMapper.VTKDelete();
        coneMapper = null;
        coneActor.VTKDelete();
        coneActor = null;

        renWin.release();
    }
}

******************************** vtkPanel.java ********************************
...
added method:

    public void release()
    {
        Lock();
        rw.RemoveRenderer(ren);
        ren.VTKDelete();
        rw.VTKDelete();
        UnLock();
    }

______________________________________________________________________________
Erdbeben im Iran: Zehntausende Kinder brauchen Hilfe. UNICEF hilft den
Kindern - helfen Sie mit! https://www.unicef.de/spe/spe_03.php




More information about the vtkusers mailing list