• Subcomponent:
    • Resolved In Build:
    • CPU:
      x86, ppc
    • OS:
      generic, windows_95, windows_nt


      I have a small piece of code, of which the main body looks like...

      public class MyApplet extends java.applet.Applet
      public void paint(Graphics g)
      Image image = createImage(200,200);
      Graphics newG = image.getGraphics();
      System.out.println("Image = " + image);

      It seems that in repetitive calling of this method, there is a memory
      leak...after time, the system crashes with memory errors, this being
      true on the Mac, Windows NT and Windows 95 version of the JDK 1.02
      Looking at the text output, this can be seen - each image buffer is
      reserved at a higher memory location than the one before.

      Below is a small java Application that creates a Frame and draws a simple
      image to it, via an off-screen drawing process.
      I have compiled and run this test application on the following machines,
      with the following results

      PowerMac 7600, 32Mb. MacOS 7.5.5 Sun JDK 1.02 Crashes after a minute
      PowerMac 7600, 32Mb. MacOS 7.5.5 Symantec Cafer DR2.1 Crashes after a minute

      Sun Sparc 10, 64Mb Sun JDK 1.02 - Runs OK .. garbage collector catches
      up eventually

      Windows 95 Box, 32Mb. Sun JDK 1.02 Crashes after 5-10 minutes
      Windows NT Box, 32Mb. Sun JDK 1.02 Crashes after 5-10 minutes

      In all the crash cases, the image memory can be seen to rise systematically
      throughout the execution of the program. The sun machine however, shows an
      almost constant region of memory from which it allocates its offscreen
      memory, inidicating that the garbage collector is doing its job.

      PS.. As a side, notice how the mouseMove method is called continuously on
      the Macintosh even though the mouse is not being moved !

      // JAVA Test Application
      // Chris Parkinson ###@###.###

      import java.awt.*;

      public class MemoryLeak
              public static void main(String argv[])
                      MainApp app = new MainApp("Memory Leak Application", 400,400);

      class MainApp extends Frame
              int mouseX=100;
              int mouseY=100;

              public MainApp(String title, int width, int height)
                      System.out.println("Welcome to " + title);

              public void paint(Graphics g)
                      //allocate a new offscreen image of the size of this window
                      Image backScreen = createImage(size().width, size().height);
                      System.out.println("New OffScreen Image Reserved : " +

                      //Now get a graphics context for where ever we are drawing to
                      Graphics theG = backScreen.getGraphics();

                      //OK, now we can clear the backscreen
                      theG.fillRect(0,0,size().width, size().height);

                      //and draw a few circles on it, surrounding the current
      mouse poisiton
                      for (int radius = 2; radius <=20; radius = radius +4)
                              theG.drawOval(mouseX-radius, mouseY-radius,
      radius*2, radius*2);

                      //and finally we can copy the offscreen image to the front
                      g.drawImage(backScreen, 0,0, this);

              public void update(Graphics g)
                      //Dont do anthing apart from call paint

              public boolean mouseMove(Event e, int x, int y)
                      //mouse drag has been detected over our frame
                      //Store the coordinates and do a repaint
                      return true; //handled





            • Assignee:
              stewilso Steve Wilson (Inactive)
              mchansunw Mei Chan (Inactive)
            • Votes:
              0 Vote for this issue
              1 Start watching this issue


              • Created: