Memory leak in Radar App

Hi All,

We are having trouble with a memory leak in a real time radar application.

The attached program (RadarMemoryLeak.java) also demonstrates the problem.

The program starts up, reads a single radar image (from the "radar.dat"
file attached), and creates an loop of 18 images from this one image (to
simulate a 3 hour loop of 10 minute images).

If you then hit the "enter key" (at the unix/dos prompt) the program
then simulates new data arriving by:
* resetting all the data (the 18 images) in the loop
* calls setData on the data reference
* loops 1000 times (the "i loop")
* then an outer loop is called up to 10000 times (the "j loop")

On all platforms (Linux, Windows and AIX) I have tested the program will
eventually fail with an out of memory error.
It occurs for both Java 1.4.2/Java3D 1.3.1 and Java 1.5/Java3D 1.4.0 beta.

The number of loops required to crash the program is highly variable.
I've seen crashes for values of j:
* as small as 1 (ie 1000 calls to setData)
* a lot around the 20 to 30 mark
* a few in the hundreds
* rarely more than 1000.

So be patient if you are trying it out.

Can anybody shed any light on what the cause of the memory leak might be?

Many thanks,

James

Attached are:
RadarMemoryLeak.java
radar.dat

To compile/run: download both files to the same directory, then:
javac RadarMemoryLeak.java
java RadarMemoryLeak

Also are attached are text output from from a memory debugger:
* start.txt: the state of the heap memory just after startup
* end.txt: the state of the heap after the "out of memory" error



--
James Kelly
Meteorological Systems Section                    Bureau of Meteorology
PO Box 1289K                                  Melbourne 3001, Australia
Phone: 61-3-9669-4724 Fax: 61-3-9669-4128     Email: J.Kelly@xxxxxxxxxx




import visad.*;
import visad.bom.RadarAdapter;
import visad.java3d.*;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.rmi.RemoteException;
import javax.swing.JFrame;

public class RadarMemoryLeak {

  public static void main(String[] args) throws VisADException, RemoteException 
{
    String radarSource = "radar.dat";
    RadarAdapter ra = null;
    try {
        ra = new RadarAdapter(-34.9f, 138.5f, 4.0f, radarSource, false);
    } catch (Exception e) {
      System.err.println("Caught Exception for \"" + radarSource + "\": " + e);
      System.exit(1);
    }

    FlatField radar = ra.getData();
    FunctionType radar_image = (FunctionType) radar.getType();
    RealTupleType radaz = radar_image.getDomain();
    RealType reflection = (RealType) radar_image.getRange();
    RealType azimuth = (RealType) radaz.getComponent(1);
    RealType range = (RealType) radaz.getComponent(0);

    int num_radar_images = 18;

    RealType index = new RealType("index");
    RealType[] indexType = { index };
    RealTupleType indexTupleType = new RealTupleType(indexType);
    SetType indexSetType = new SetType( indexTupleType );
    FunctionType radar_time_series = new FunctionType(indexTupleType, 
radar_image);
    Integer1DSet indexSet = new Integer1DSet(indexSetType, num_radar_images);
    FieldImpl radar_times = new FieldImpl(radar_time_series, indexSet);
    for (int i = 0; i < num_radar_images; i++) {
        System.out.println("i = " + i);
        radar_times.setSample(i, radar);
    }

    DisplayImplJ3D display = new DisplayImplJ3D("radar");
    ScalarMap lonmap = new ScalarMap(RealType.Longitude, Display.XAxis);
    display.addMap(lonmap);
    ScalarMap latmap = new ScalarMap(RealType.Latitude, Display.YAxis);
    display.addMap(latmap);
    display.addMap(new ScalarMap(RealType.Altitude, Display.ZAxis));
    ScalarMap rgbMap = new ScalarMap(reflection, Display.RGB);
    // ScalarMap rgbMap = new ScalarMap(reflection, Display.RGBA);
    // rgbMap.setRange(0.,6.);
    display.addMap(rgbMap);

    GraphicsModeControl mode = display.getGraphicsModeControl();
    mode.setScaleEnable(true);

    DataReference ref = new DataReferenceImpl("radar_ref");
    ref.setData(radar_times);
    display.addReference(ref);

    JFrame frame = new JFrame("VisAD BOM radar image");
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });

    frame.getContentPane().add(display.getComponent());
    int WIDTH = 500;
    int HEIGHT = 600;

    frame.setSize(WIDTH, HEIGHT);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(screenSize.width/2 - WIDTH/2,
                      screenSize.height/2 - HEIGHT/2);
    frame.setVisible(true);
    try {
        System.in.read();
    } catch (java.io.IOException ioe) {
    }


    for (int j = 0; j < 10000; j++) {
      display.disableAction();
      for (int i = 0; i < 1000; i++) {
        System.out.println("i = " + i);
        for (int k = 0; k < num_radar_images; k++) {
                radar_times.setSample(k, radar);
        }
        ref.setData(radar_times);
      }
      display.enableAction();
      System.out.println("********************** j = " + j);
    }
  }
}






Attachment: radar.dat
Description: MPEG movie

Runtime Heap Summary
===================

Runtime Instance List
---------------------

    Package        Class                        Count            Memory         
    -------        -----                        -----            ------         
                   Total                     1,777             88,296    
                   double[ ]                   289  (16.3%)    25,168  (28.5%)  
  
                   byte[ ]                      82   (4.6%)    17,632  (20.0%)  
  
          visad    DisplayEvent                 84   (4.7%)     6,720   (7.6%)  
  
                   char[ ]                      57   (3.2%)     4,832   (5.5%)  
  
 java.awt.event    MouseEvent                   77   (4.3%)     4,312   (4.9%)  
  
        sun.awt    MostRecentKeyValue          259  (14.6%)     4,144   (4.7%)  
  
                   Total Object[ ]             145   (8.2%)     3,896   (4.4%)  
  
javax.media.j3d    Transform3D                  81   (4.6%)     2,592   (2.9%)  
  
  java.lang.ref    WeakReference               107   (6.0%)     2,568   (2.9%)  
  
       java.awt    EventQueueItem               90   (5.1%)     2,160   (2.4%)  
  
      java.util    HashMap$Entry                70   (3.9%)     1,680   (1.9%)  
  
        sun.awt    EventQueueItem               90   (5.1%)     1,440   (1.6%)  
  
  javax.vecmath    Point3d                      44   (2.5%)     1,408   (1.6%)  
  
          visad    ControlEvent                 14   (0.8%)     1,008   (1.1%)  
  
      java.lang    String                       38   (2.1%)       912   (1.0%)  
  
      java.util    Vector                       30   (1.7%)       720   (0.8%)  
  
      java.util    Vector$1                     34   (1.9%)       544   (0.6%)  
  
                   int[ ]                       11   (0.6%)       496   (0.6%)  
  
                   short[ ]                      6   (0.3%)       496   (0.6%)  
  
javax.media.j3d    AppearanceRetained            4   (0.2%)       448   (0.5%)  
  
 java.awt.event    InvocationEvent               8   (0.5%)       384   (0.4%)  
  
      java.lang    StringBuffer                 13   (0.7%)       312   (0.4%)  
  
      java.util    LinkedList$Entry             12   (0.7%)       288   (0.3%)  
  
       java.awt    Dimension                    18   (1.0%)       288   (0.3%)  
  
       java.awt    SentEvent                     7   (0.4%)       280   (0.3%)  
  
 java.awt.event    FocusEvent                    8   (0.5%)       256   (0.3%)  
  
javax.media.j3d    ColoringAttributesRetained    4   (0.2%)       256   (0.3%)  
  
javax.media.j3d    Appearance                    4   (0.2%)       192   (0.2%)  
  
  javax.vecmath    Vector3d                      6   (0.3%)       192   (0.2%)  
  
      java.util    ArrayList                     8   (0.5%)       192   (0.2%)  
  
javax.media.j3d    ColoringAttributes            4   (0.2%)       192   (0.2%)  
  
  javax.vecmath    Color3f                       8   (0.5%)       192   (0.2%)  
  



Runtime Heap Summary
===================

Runtime Instance List
---------------------

    Package                      Class       Count               Memory         
  
    -------                      -----       -----               ------         
  
                    Total                7,585             60,176,264 
                    double[ ]                1   (3.2%)    28,176,376  (46.8%)  
  
                    float[ ]                 1   (1.5%)    22,272,592  (37.0%)  
  
                    byte[ ]                  7   (0.5%)     9,440,392  (15.7%)  
  
                    Total Object[ ]          2  (27.3%)        50,064   (0.1%)  
  
   javax.vecmath    Point3d                  5   (7.8%)        19,040   (0.0%)  
  
                    int[ ]                   6   (3.2%)        18,024   (0.0%)  
  
       java.util    ArrayList                4   (8.9%)        16,176   (0.0%)  
  
       java.util    Vector                   0   (5.0%)         9,120   (0.0%)  
  
 javax.media.j3d    Texture2DRetained        6   (0.5%)         8,352   (0.0%)  
  
 javax.media.j3d    Shape3DRetained          6   (0.5%)         8,352   (0.0%)  
  
 javax.media.j3d    TriangleStripArrayRetaine8   (0.2%)         7,776   (0.0%)  
  
 javax.media.j3d    AppearanceRetained       5   (0.9%)         7,280   (0.0%)  
  
 javax.media.j3d    BranchGroupRetained      7   (0.5%)         7,104   (0.0%)  
  
   javax.vecmath    Color3f                  2   (3.6%)         6,528   (0.0%)  
  
      sun.java2d    SunGraphics2D            2   (0.4%)         6,144   (0.0%)  
  
           visad    FlatField                7   (0.8%)         5,928   (0.0%)  
  
 javax.media.j3d    BoundingBox              8   (1.4%)         5,184   (0.0%)  
  
           visad    DisplayEvent             9   (0.8%)         4,720   (0.0%)  
  
 javax.media.j3d    RenderMolecule           8   (0.2%)         4,608   (0.0%)  
  
 javax.media.j3d    TextureAttributesRetained6   (0.5%)         4,320   (0.0%)  
  
 javax.media.j3d    MaterialRetained         4   (0.7%)         4,320   (0.0%)  
  
 javax.media.j3d    ImageComponent2DRetained 8   (0.2%)         3,888   (0.0%)  
  
 javax.media.j3d    PolygonAttributesRetained4   (0.7%)         3,888   (0.0%)  
  
 javax.media.j3d    LineAttributesRetained   4   (0.7%)         3,888   (0.0%)  
  
 javax.media.j3d    TransparencyAttributesRet4   (0.7%)         3,888   (0.0%)  
  
 javax.media.j3d    PointAttributesRetained  4   (0.7%)         3,456   (0.0%)  
  
           visad    ThingChangedEvent        2   (0.6%)         3,360   (0.0%)  
  
        java.awt    Rectangle                1   (1.7%)         3,144   (0.0%)  
  
 javax.media.j3d    Transform3D              2   (1.2%)         2,944   (0.0%)  
  
       java.util    HashMap                  2   (0.9%)         2,880   (0.0%)  
  
 javax.media.j3d    RenderingAttributesRetain6   (0.5%)         2,592   (0.0%)  
  
  java.awt.event    MouseEvent               2   (0.6%)         2,352   (0.0%)  
  
       java.util    Vector$1                 6   (1.9%)         2,336   (0.0%)  
  
 javax.media.j3d    TextureUnitStateRetained 6   (0.5%)         2,304   (0.0%)  
  
   java.lang.ref    Finalizer                2   (0.9%)         2,304   (0.0%)  
  
 javax.media.j3d    Appearance               7   (0.6%)         2,256   (0.0%)  
  
   java.awt.geom    AffineTransform          2   (0.4%)         2,048   (0.0%)  
  
 sun.java2d.pipe    Region                   4   (0.8%)         2,048   (0.0%)  
  
 javax.media.j3d    ColoringAttributesRetaine9   (0.4%)         1,856   (0.0%)