xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hans Stoessel" <hstoes...@pm-medici.ch>
Subject Re: JSVGCanvas: java.lang.OutOfMemoryError
Date Wed, 09 Apr 2003 12:35:22 GMT
Hi

I don't think, that I hold a reference to JSVGCanvas in my application. I
think, its a problem of JSVGCanvas, but I'm not sure of course. I send you
the whole class, where I use JSVGCanvas, its the only class I use it. After
the class, you will find the only implementation of this class in my
application. Every time I display a SVG file, I create a class DialogChart
and show it. After that, I set the Dialog oDialogChart to null. At least,
you will find a routine for garbage collection. I call this routine several
times in my application, also after I set oDialogChart to null.

This is all I do with JSVGCanvas and I can see that the memory is increased
and increased. I don't know why. Maybe you have an idea.

How do you use JSVGCanvas? Maybe you have a short code example for me.

Thanks for the help.

Hans


//**************************************************************************
**
// Begin class DialogChart and some subclasses
//**************************************************************************
**

package ch.pmm.chartstudio.chartgen;

import ch.pmm.chartstudio.*;
import ch.pmm.chartstudio.chartstudio.ChartStudio;
import ch.pmm.util.StringFormat;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.File;

// Batik
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter;
import org.apache.batik.swing.svg.SVGDocumentLoaderEvent;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.svg.SVGDocumentFactory;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.apache.batik.swing.gvt.GVTTreeRendererAdapter;
import org.apache.batik.swing.gvt.GVTTreeRendererEvent;

import org.w3c.dom.svg.SVGDocument;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: PM Medici AG</p>
 * @author Oliver Hirschi
 * @version 1.0
 */

public class DialogChart extends Dialog1 {
  private JPanel                  m_oJPanelChart;
  private PanelDialogChart_Button m_oButton;
 private String            m_sFilenameChart = null;
  private JLabel                  m_oJLabelState;
  private JTextField              m_oJTextFieldState;
  private SVGDocument       m_oSVGDocument = null;
 private JSVGCanvas        m_oSVGCanvas = null;



//--------------------------------------------------------------------------
 // Konstruktor

//--------------------------------------------------------------------------
  public DialogChart(Application oApp, String sFilenameChart) {
    super(oApp, Constant.LANG_DIALOG_CHARTGENERATE);

    m_oDialogSize = new Dimension(Constant.DIALOGDYNTEXT_X,
Constant.DIALOGDYNTEXT_Y);
    m_sFilenameChart = sFilenameChart;

    m_oLanguage = new Language(Constant.LANG_DIALOG_CHART);
    try {
      jbInit();
      m_bDialogInit = true;
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }


//--------------------------------------------------------------------------
 // Filename zurückgeben

//--------------------------------------------------------------------------
 public String getFilenameChart() { return m_sFilenameChart; }

 public SVGDocument readSVGDocument(String uri) {
    SVGDocumentFactory df = new
SAXSVGDocumentFactory("org.apache.crimson.parser.XMLReaderImpl");

    try {
   return df.createSVGDocument(uri);
    }
    catch (java.io.IOException ioe) {
      ioe.printStackTrace();
    }
    return null;
}

//--------------------------------------------------------------------------
 // Dialog initialisieren

//--------------------------------------------------------------------------
  private void jbInit() throws Exception {
   ChartStudio oApp = (ChartStudio) getApp();

    // Rand
    Border oBorderLabel = new EmptyBorder(0, 0, 0, 10);
    // Panel
    m_oJPanelChart = new JPanel(new GridBagLayout());
    m_oJPanelChart.setBorder(new TitledBorder(new EtchedBorder()));
    // Constraints
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.anchor = GridBagConstraints.WEST;
    // SVG-File
    m_oSVGCanvas = new JSVGCanvas();

    // Set the JSVGCanvas listeners.
    m_oSVGCanvas.addSVGDocumentLoaderListener(new
DialogChartSVGDocumentLoaderAdapter(this));
    m_oSVGCanvas.addGVTTreeBuilderListener(new
DialogChartGVTTreeBuilderAdapter(this));
    m_oSVGCanvas.addGVTTreeRendererListener(new
DialogChartGVTTreeRendererAdapter(this));

    //m_oSVGCanvas.setURI(new File(m_sFilenameChart).toURL().toString());
  m_oSVGDocument = readSVGDocument(new
File(m_sFilenameChart).toURL().toString());
    m_oSVGCanvas.setSVGDocument(m_oSVGDocument);
    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 2;
    m_oJPanelChart.add(m_oSVGCanvas, c);
    // Label Schriftstil
    c.gridx = 0;
    c.gridy++;
    c.gridwidth = 1;
    m_oJLabelState = new JLabel(m_oLanguage.getText(1051) + ":");
    m_oJLabelState.setBorder(oBorderLabel);
    m_oJPanelChart.add(m_oJLabelState, c);
    // Textfeld Schriftstil
    c.gridx++;
    m_oJTextFieldState = new JTextField(Constant.DIALOG_COLUMN_LEN30);
    m_oJTextFieldState.setEnabled(false);
    m_oJPanelChart.add(m_oJTextFieldState, c);
    // Buttons
    m_oButton = new PanelDialogChart_Button();
    // Dialog bauen
    JPanel oJPanel = new JPanel(new BorderLayout());
    oJPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
    oJPanel.add(m_oJPanelChart, BorderLayout.CENTER);
    oJPanel.add(m_oButton, BorderLayout.SOUTH);
    setTitle(m_oLanguage.getText(1050));
    getContentPane().add(oJPanel);
    setResizable(false);
    centerWindow();
  }


//--------------------------------------------------------------------------
 // Statustext setzen

//--------------------------------------------------------------------------
 public void setStateText(int iText) {
    m_oJTextFieldState.setText(m_oLanguage.getText(iText));
 }


//--------------------------------------------------------------------------
 // SVG-Datei eingelesen -> Aufbau des Dialog beendet

//--------------------------------------------------------------------------
 public void finishDialog() {
    m_oJPanelChart.remove(m_oJLabelState);
    m_oJPanelChart.remove(m_oJTextFieldState);
    pack();
    centerWindow();
 }
}

//--------------------------------------------------------------------------
--
// Panel mit Buttons
//--------------------------------------------------------------------------
--
class PanelDialogChart_Button extends Panel2 {

//--------------------------------------------------------------------------
 // Konstruktor

//--------------------------------------------------------------------------
  public PanelDialogChart_Button() {
    super();
    m_oJButtonOK.setVisible(false);
    m_oJButtonCancel.setText(Language.END);
  }


//--------------------------------------------------------------------------
 // Action behandeln

//--------------------------------------------------------------------------
  public void actionPerformed(ActionEvent e) {
    DialogChart oDialog = (DialogChart) getDialog();
    if (e.getSource() == m_oJButtonCancel) {
   File oFile = new File(oDialog.getFilenameChart());
   if (oFile.exists()) oFile.delete();
      oDialog.setAction(Constant.ACTION_CANCEL);
      oDialog.setVisible(false);
    }
  }
}

//--------------------------------------------------------------------------
--
// GVTTreeRendererAdapter
//--------------------------------------------------------------------------
--
class DialogChartGVTTreeRendererAdapter extends GVTTreeRendererAdapter {
 DialogChart m_oDialogChart = null;

//--------------------------------------------------------------------------
 // Konstruktor

//--------------------------------------------------------------------------
 public DialogChartGVTTreeRendererAdapter(DialogChart oDialogChart) {
  super();
  m_oDialogChart = oDialogChart;
 }


//--------------------------------------------------------------------------
 // Rendering starten

//--------------------------------------------------------------------------
  public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
  m_oDialogChart.setStateText(1056);
  }


//--------------------------------------------------------------------------
 // Rendering beenden

//--------------------------------------------------------------------------
  public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
  m_oDialogChart.setStateText(1057);
  m_oDialogChart.finishDialog();
  }
}

//--------------------------------------------------------------------------
--
// SVGDocumentLoaderAdapter
//--------------------------------------------------------------------------
--
class DialogChartSVGDocumentLoaderAdapter extends SVGDocumentLoaderAdapter {
 DialogChart m_oDialogChart = null;


//--------------------------------------------------------------------------
 // Konstruktor

//--------------------------------------------------------------------------
 public DialogChartSVGDocumentLoaderAdapter(DialogChart oDialogChart) {
  super();
  m_oDialogChart = oDialogChart;
 }


//--------------------------------------------------------------------------
 // Dokument laden beginnt

//--------------------------------------------------------------------------
  public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
    m_oDialogChart.setStateText(1052);
  }


//--------------------------------------------------------------------------
 // Dokument laden beendet

//--------------------------------------------------------------------------
  public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
    m_oDialogChart.setStateText(1053);
  }
}

//--------------------------------------------------------------------------
--
// GVTTreeBuilderAdapter
//--------------------------------------------------------------------------
--
class DialogChartGVTTreeBuilderAdapter extends GVTTreeBuilderAdapter {
 DialogChart m_oDialogChart = null;


//--------------------------------------------------------------------------
 // Konstruktor

//--------------------------------------------------------------------------
 public DialogChartGVTTreeBuilderAdapter(DialogChart oDialogChart) {
  super();
  m_oDialogChart = oDialogChart;
 }


//--------------------------------------------------------------------------
 // Baum wird aufgebaut

//--------------------------------------------------------------------------
  public void gvtBuildStarted(GVTTreeBuilderEvent e) {
    m_oDialogChart.setStateText(1054);
  }


//--------------------------------------------------------------------------
 // Baum fertig

//--------------------------------------------------------------------------
  public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
    m_oDialogChart.setStateText(1055);
  }
}

//**************************************************************************
**
// End class DialogChart and some subclasses
//**************************************************************************
**


//**************************************************************************
**
// Begin Implementation
//**************************************************************************
**

DialogChart oDialogChart = new DialogChart(oDialog.getApp(),
oDialogChartGenerate.getProdJob().getSVGPath(true));
oDialogChart.show();
oDialogChart = null;
Application.gc();

//**************************************************************************
**
// End Implementation
//**************************************************************************
**


//**************************************************************************
**
// Begin Application.gc
//**************************************************************************
**

//--------------------------------------------------------------------------
// Garbage Coolection
//--------------------------------------------------------------------------
public static void gc() {
 Runtime rt = Runtime.getRuntime();
 long    isFree = rt.freeMemory();
 long    wasFree = 0;

 do {
  wasFree = isFree;
  rt.runFinalization();
  rt.gc();
  isFree = rt.freeMemory();
 } while (isFree > wasFree);
}

//**************************************************************************
**
// End Application.gc
//**************************************************************************
**



"Thomas E Deweese" <thomas.deweese@kodak.com> schrieb im Newsbeitrag
news:16020.817.728658.44321@frog.rl.kodak.com...
> >>>>> "HS" == Hans Stoessel <hstoessel@pm-medici.ch> writes:
>
> HS> Hi I use the following source code to display a svg file with a
> HS> chart in a dialog:
>
>     You are only showing the init code.  Since you create a new
> JSVGCanvas each time, I suspect that you are somehow holding on to a
> reference to the JSVGCanvas (or the dialog) somewhere in your code
> (often this can be hidden in a listener or something).
>
>     While I can't rule out memory leaks in Batik (in fact I'd be
> surprised if there weren't at least a few) I don't think there are
> _gross_ memory leaks (I often view >100 SVG files in squiggle w/o a
> problem).
>
> HS> If I do that a few times, then I have the java error:
> HS> java.lang.OutOfMemoryError <<no stack trace available>>
>
> HS> I use Batik 1.5 Beta 5 and Windows 2000. If I have a look in the
> HS> task manager of windows, I see that the memory is increasing with
> HS> every time I display a SVG file in my dialog.
>
> HS> What could be wrong?
>
> HS> Thanks for help




---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Mime
View raw message