JB中可以Force GC
自己也可以调用System.gc(),但是要先保证把不用的资源先关闭

解决方案 »

  1.   

    下面是代码!!!
    我下面已经改了啊!!!!!但还是不行啊!!!注意看那个clean()函数啊 
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.net.*;
    import java.awt.image.*; public class Trive extends JApplet implements MouseInputListener{
       private String string;//****判断操作的变量****
      
       private ButtonGroup group = new ButtonGroup();
       private JPanel jPanel1 = new JPanel();
       private JLabel lbDrag = new JLabel();
       private Panel Panel1 = new Panel();
       private JScrollPane jsp = new JScrollPane();
       JToggleButton jToggleButton1 = new JToggleButton();
       JToggleButton jToggleButton2 = new JToggleButton();
       JToggleButton jToggleButton3 = new JToggleButton();
       JToggleButton jToggleButton4 = new JToggleButton();
       JToggleButton jToggleButton5 = new JToggleButton();
       JToggleButton jToggleButton6 = new JToggleButton();
       JToggleButton jToggleButton7 = new JToggleButton();
       
       Point p=new Point(0,0);
       Point newP;   int nwidth,nheight;//最初坐标
       int swidth,sheight;//复位坐标
       int ewidth,eheight;//缩小放大,放大和缩小的共用变量
       
       boolean flag;
       
       Icon map ;
       Image img,img2,imageqt,imagesq;//img2是image转变的中间变量,img图片交换的中间量
     
       ImageFilter stretchF;
       FilteredImageSource stretchS;
       
       public void init() {
          
        Panel1.setBounds(new Rectangle(2, 1, 500, 28));
        jPanel1.setLayout(new GridLayout(1,11));
        jToggleButton1.setText("放大");
        jToggleButton1.setBounds(new Rectangle(0, 3, 60, 26));
        jToggleButton2.setText("缩小");
        jToggleButton2.setBounds(new Rectangle(59, 4, 60, 26));
        jToggleButton3.setText("移动");
        jToggleButton3.setBounds(new Rectangle(113, 4, 60, 26));
        jToggleButton4.setText("复位");
        jToggleButton4.setBounds(new Rectangle(170, 4, 60, 26));
        jToggleButton5.setText("鹰眼");
        jToggleButton5.setBounds(new Rectangle(228, 4, 60, 26));
        jToggleButton6.setText("市区");
        jToggleButton6.setBounds(new Rectangle(228, 4, 60, 26));
        jToggleButton7.setText("全图");
        jToggleButton7.setBounds(new Rectangle(228, 4, 60, 26));
        group.add(jToggleButton1);
        group.add(jToggleButton2);
        group.add(jToggleButton3);
        group.add(jToggleButton4);
        group.add(jToggleButton5);
        group.add(jToggleButton6);
        group.add(jToggleButton7);
        Panel1.add(jToggleButton1);
        Panel1.add(jToggleButton2);
        Panel1.add(jToggleButton3);
        Panel1.add(jToggleButton4);
        Panel1.add(jToggleButton5);
        Panel1.add(jToggleButton6);
        Panel1.add(jToggleButton7);
        this.getContentPane().add(Panel1,BorderLayout.NORTH);
        jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        String imagesqfile = getParameter("picturesq");
        String imageqtfile = getParameter("pictureqt");
        
        img = getImage(getDocumentBase(),imagesqfile);//初始化界面的图片,用img变量
        
        imagesq = getImage(getDocumentBase(),imagesqfile);//得到地图*******
        imageqt = getImage(getDocumentBase(),imageqtfile);//得到地图*******
          
          createIcon();
          
          setewAndeh();
          
          lbDrag.setIcon(map);
          lbDrag.setBounds(-50,-50, ewidth,eheight);
          this.getContentPane().add(jsp, BorderLayout.CENTER);
          jsp.getViewport().add(jPanel1,null);
          jPanel1.add(lbDrag,null);
          lbDrag.addMouseListener(this);
          lbDrag.addMouseMotionListener(this);
          jToggleButton6.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e)
               {
                  changMap(imagesq);
               }
             }
          );
           jToggleButton7.addActionListener(
             new ActionListener(){
               public void actionPerformed(ActionEvent e)
               {
                  changMap(imageqt);
               }
             }
          );
          jToggleButton2.addActionListener(
            new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
                 setString(e.getActionCommand());
                 if(flag) 
                 setewAndeh();
                 flag = false;
              }
            }
          );
          jToggleButton4.addActionListener(
            new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
               setString(e.getActionCommand());
               recover();     
               flag = true;
              }
            }
          );
          jToggleButton1.addActionListener(
            new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
                setString(e.getActionCommand());
                 if(flag) 
                 setewAndeh();
                 flag = false;          }
            }
          );
          jToggleButton3.addActionListener(
            new ActionListener(){
              public void actionPerformed(ActionEvent e)
              {
                setString(e.getActionCommand());          }
            }
          );
       System.gc();//调用GC了啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1但还是不行啊
       }
       public void mouseMoved(MouseEvent e){}
       public void mouseDragged(MouseEvent e){
           if(string =="移动")
           {
             newP=SwingUtilities.convertPoint(
                   lbDrag,e.getPoint(),lbDrag.getParent());
             lbDrag.setLocation(lbDrag.getX()+(newP.x-p.x),
                              lbDrag.getY()+(newP.y-p.y));
             p=newP;
           }
       }
       public void mouseReleased(MouseEvent e){}
       public void mouseEntered(MouseEvent e){}
       public void mouseExited(MouseEvent e){}
       public void mouseClicked(MouseEvent e){
          clean();
          if(string =="缩小")
          makeSize(string);
          if(string=="放大")
          makeSize(string);
         
       }
       public void mousePressed(MouseEvent e){
           if(string =="移动")
           {
             p=SwingUtilities.convertPoint(lbDrag,e.getPoint(),lbDrag.getParent());
           }
       }
      //****交换地图******
       public void changMap(Image image)
       {
          img = null;//释放内存
          img = image;
          createIcon();
          lbDrag.setIcon(map);
       }
      //*****初始化界面的图片*****
       public void createIcon()
       {
         getWandH(img);
         setWandH(nwidth,nheight);
         create(swidth,sheight);
       }
      //*****设置适当的宽度和高度*****
       public void setWandH( int width ,int height)
       {
         swidth = width/4 ;
         sheight = height/4+10;
       }
       //*****初始放大和缩小的,宽度和高度*****
       public void setewAndeh()
       {
         ewidth = swidth;
         eheight = sheight;
       }
      

  2.   

    //*****判别操作*****
       public void setString (String str)
       {
         string = str;
       }
       //*****取得原始的高度和宽度*****
       public void getWandH(Image image)
       {
          nwidth = image.getWidth(this);
          nheight = image.getHeight(this);
       }
      //*****设置放大的基数*****
       public void extendWandH()
       {
         ewidth = ewidth+50;
         eheight = eheight+50;
       }
       //*****设置缩小的基数*****
       public void subWandH()
       {
          ewidth = ewidth -35;
          eheight = eheight-35;
       }
       //对图片的缩小或是放大的操作
       public void makeSize(String string)
       {
         String chooseStr = string ;
         
         if(chooseStr == "放大")
         extendWandH();
         
         if(chooseStr == "缩小")
         subWandH();
         
         lbDrag.setBounds(lbDrag.getX()+(newP.x-p.x),lbDrag.getY()+(newP.y-p.y), ewidth,eheight);
         create(ewidth,eheight);
         lbDrag.setIcon(map);
       }
      //*****复位操作*****
       public void recover()
       {
          getWandH(img);
          setWandH( nwidth , nheight);
          create(swidth,sheight);
          lbDrag.setIcon(map);
       }
       //*****把image转化为icon*****
       public void create(int width,int height)
       {
          int cwidth = width ;
          int cheight = height;
          clean();//调用clean()来释放内存
          stretchF = new ReplicateScaleFilter(cwidth,cheight);
          stretchS = new FilteredImageSource(img.getSource(),stretchF);
          img2 = createImage(stretchS);
          map = new ImageIcon(img2); 
       }
       //*****释放内存*****
       public void clean()
       {
         stretchF = null;
         stretchS = null;
         map = null;
         img2 = null;
       }
      
    }
      
      

  3.   

    //*****判别操作*****
       public void setString (String str)
       {
         string = str;
       }
       //*****取得原始的高度和宽度*****
       public void getWandH(Image image)
       {
          nwidth = image.getWidth(this);
          nheight = image.getHeight(this);
       }
      //*****设置放大的基数*****
       public void extendWandH()
       {
         ewidth = ewidth+50;
         eheight = eheight+50;
       }
       //*****设置缩小的基数*****
       public void subWandH()
       {
          ewidth = ewidth -35;
          eheight = eheight-35;
       }
       //对图片的缩小或是放大的操作
       public void makeSize(String string)
       {
         String chooseStr = string ;
         
         if(chooseStr == "放大")
         extendWandH();
         
         if(chooseStr == "缩小")
         subWandH();
         
         lbDrag.setBounds(lbDrag.getX()+(newP.x-p.x),lbDrag.getY()+(newP.y-p.y), ewidth,eheight);
         create(ewidth,eheight);
         lbDrag.setIcon(map);
       }
      //*****复位操作*****
       public void recover()
       {
          getWandH(img);
          setWandH( nwidth , nheight);
          create(swidth,sheight);
          lbDrag.setIcon(map);
       }
       //*****把image转化为icon*****
       public void create(int width,int height)
       {
          int cwidth = width ;
          int cheight = height;
          clean();//调用clean()来释放内存
          stretchF = new ReplicateScaleFilter(cwidth,cheight);
          stretchS = new FilteredImageSource(img.getSource(),stretchF);
          img2 = createImage(stretchS);
          map = new ImageIcon(img2); 
       }
       //*****释放内存*****
       public void clean()
       {
         stretchF = null;
         stretchS = null;
         map = null;
         img2 = null;
       }
      
    }
      
      

  4.   

    我在上面的那个
     //*****释放内存*****
       public void clean()
       {
         System.gc();//这是加进去的啊
         stretchF = null;
         stretchS = null;
         map = null;
         img2 = null;
       }
    但还是不行啊!!!!!
      

  5.   

    System.gc()的API解释是:
    Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects. 
    只是建议JVM实行GC,但在哪个时候执行并不可知!只是有一定作用,作用并不明显的,使用中内存占用有些增大也是正常的:)
      

  6.   

    但是很大的啊!!
    是经常会死机的啊!!!
    那个applet只是操作了几下就,内存就占用了差不多120m左右!!!
    楼上的大哥能不能帮我看一下那个代码啊!!
    我今天改了一天都没有结果!!!!