[visad] Why does my application eat up the memory?

Dear all:
   The attach file is my java demo application. When I press the 'loop' button, 
my system's memory is eaten up by it. Could you find the reason? Or how can I 
avoid this?

thanks,

Best,
lizhi
##########################################################################
  Key Laboratory of Regional Climate-Environment for East Asia,
  START/RRC Temperate East Asia, Institute of
  Atmospheric Physics, Chinese Academy of Sciences

  Tel(O)  :  86-10-82995140
  Fax(O)  :  86-10-82995135
  E-mail  :  wlz@xxxxxxxxx
  Address :  P.O.Box 9804,Beijing 100029,  P.R.China
##########################################################################
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import visad.*;
import visad.util.*;
import visad.java2d.*;
import visad.java3d.*;

import java.rmi.RemoteException;

public class GLayer extends DisplayImplJ3D
{
   public GLayer( String name, float[][][][] datas, float[][][] depth )
        throws VisADException, RemoteException
   {
      super( name );

      _datas = datas;
      _depth = depth;

      init();

      ContourControl isoControl = (ContourControl)_isomap.getControl();
      isoControl.setContourFill( true );
   }

   public void next()
   {
System.err.println( _inx );
      _inx ++;
      if( _inx >= _datas.length )
         _inx = 0;

      try
      {
         update();
      }
      catch( Exception e )
      {
         e.printStackTrace();
      }
   }

   private void init()
        throws VisADException, RemoteException
   {
      int NX = _depth[0][0].length;
      int NY = _depth[0].length;
      int NZ = _depth.length;

      RealType x = RealType.getRealType( "X" );
      RealType y = RealType.getRealType( "Y" );
      RealType z = RealType.getRealType( "Z" );
      RealType v = RealType.getRealType( "Value" );
      RealType[] time = { RealType.Time };

      ScalarMap xmap = new ScalarMap( x, Display.XAxis );
      ScalarMap ymap = new ScalarMap( y, Display.YAxis );
      ScalarMap zmap = new ScalarMap( z, Display.ZAxis );
      _vmap          = new ScalarMap( v, Display.RGBA );
      _isomap        = new ScalarMap( v, Display.IsoContour );

      addMap( xmap );
      addMap( ymap );
      addMap( zmap );
      addMap( _vmap );
      addMap( _isomap );

      /*
      xmap.setRange( 0, NX-1 );
      ymap.setRange( 0, NY-1 );
      zmap.setRange( 0, NZ-1 );
      */

      GraphicsModeControl mode = getGraphicsModeControl();
      mode.setScaleEnable( true );
      mode.setTransparencyMode( DisplayImplJ3D.NICEST );
      mode.setTexture3DMode( GraphicsModeControl.TEXTURE3D );
      mode.setTextureEnable( true );
      
      DataReferenceImpl baseRef = new DataReferenceImpl( "Baseref" );
      addReference( baseRef, null );

      RealTupleType domain2dxy = new RealTupleType( x, y );
      RealTupleType times = new RealTupleType( time );

      Linear2DSet set2d = new Linear2DSet( domain2dxy, 0, NX-1, NX, 0, NY-1, NY 
);
      Linear1DSet set1d = new Linear1DSet( times, 0, NZ-1, NZ );

      FunctionType f2d = new FunctionType( domain2dxy, new RealTupleType( z, v 
) );
      FunctionType f1d = new FunctionType( times, f2d );

      _f2dData = new FlatField[NZ];
      for( int k = 0; k < NZ; k ++ )
         _f2dData[k] = new FlatField( f2d, set2d );
      _f1dData = new FieldImpl( f1d, set1d );

      _samples = new float[NZ][2][NY*NX];
      baseRef.setData( _f1dData );

      for( int k = 0; k < NZ; k ++ )
         for( int j = 0; j < NY; j ++ )
            for( int i = 0; i < NX; i ++ )
               _samples[k][0][j*NX+i] = _depth[k][j][i];

      update();
   }

   private void update()
        throws VisADException, RemoteException
   {
      float[][][] data = _datas[_inx];
      int NX = data[0][0].length;
      int NY = data[0].length;
      int NZ = data.length;
      float min = data[0][0][0];
      float max = min;
      for( int k = 0; k < NZ; k ++ )
         for( int j = 0; j < NY; j ++ )
            for( int i = 0; i < NX; i ++ )
            {
               _samples[k][1][j*NX+i] = data[k][j][i];
               if( min > data[k][j][i] ) min = data[k][j][i];
               if( max < data[k][j][i] ) max = data[k][j][i];
            }
System.err.println( min + " " + max );

      _vmap.setRange( min, max );

      for( int k = 0; k < NZ; k ++ )
         _f2dData[k].setSamples( _samples[k], false );

      double[] vrange = _vmap.getRange();
      _isomap.setRange( vrange[0], vrange[1] );

      for( int k = 0; k < NZ; k ++ )
         _f1dData.setSample( k, _f2dData[k] );
   }

   private int _inx = 0;

   private ScalarMap _vmap, _isomap;

   private float[][][][] _datas;
   private float[][][] _depth;

   private float[][][] _samples;
   private FlatField[] _f2dData;
   private FieldImpl _f1dData;

   public static void main( String[] argv ) throws Exception
   {
      int NX = 25;
      int NY = 25;
      int NZ = 4;
      int TIME = 10;
      float[][][][] datas = new float[TIME][NZ][NY][NX];
      float[][][] depth = new float[NZ][NY][NX];
      for( int k = 0; k < NZ; k ++ )
         for( int j = 0; j < NY; j ++ )
            for( int i = 0; i < NX; i ++ )
               depth[k][j][i] = -( 1000 + 10*k + 5*j + i );
      for( int m = 0; m < TIME; m ++ )
         for( int k = 0; k < NZ; k ++ )
            for( int j = 0; j < NY; j ++ )
               for( int i = 0; i < NX; i ++ )
                  datas[m][k][j][i] = 10*m + k + j + i;

      JFrame f = new JFrame();
      Container c = f.getContentPane();
      c.setLayout( new BorderLayout() );
      final GLayer layer = new GLayer( "Layer", datas, depth );
      c.add( layer.getComponent(), BorderLayout.CENTER );
      final javax.swing.Timer timer = new javax.swing.Timer( 1000, new 
AbstractAction()
      {
         public void actionPerformed( ActionEvent e )
         {
            layer.next();
         }
      } );
      final JButton loopBtn = new JButton( "loop" );
      final JButton stopBtn = new JButton( "stop" );
      loopBtn.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent e )
         {
            timer.start();
            loopBtn.setEnabled( false );
            stopBtn.setEnabled( true );
         }
      } );
      stopBtn.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent e )
         {
            timer.stop();
            loopBtn.setEnabled( true );
            stopBtn.setEnabled( false );
         }
      } );
      JPanel p = new JPanel();
      p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) );
      p.add( loopBtn );
      p.add( stopBtn );
      c.add( p, BorderLayout.PAGE_END );
      f.pack();
      f.setLocationRelativeTo( null );
      f.setVisible( true );
   }
}   ///:~
  • 2008 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: