title: Java - Correctly Removing an Image to avoid Memory Leaks
body: <p>As the title suggests, I'm getting memory leaks and I suspect their origin lies within the creation of new images. The memory leak is of type <strong>java.util.Hashtable$Entry</strong>. Google searches haven't provided me with anything relating to image creation.</p>
  
  <p>The project is an interpolator - the user inputs a number of points and the program joins them together with curves. When this is done, the curves are drawn on an image. This necessitates that each time the interpolation is generated, the image is recreated with the new width and height before it's drawn over. </p>
  
  <p>I believe that I've narrowed down the leak to this method:</p>
  
  <pre><code>public void reset(int width, int height){
      if (width &gt;= 15){
          i_width = width;
      } else {
          i_width = 15;
      }
      if (height &gt;= 15){
          i_height = height;
      } else { 
          i_height = 15;
      }
  
      try {
          image.flush();
      } catch (Exception e) {}
  
      image = new BufferedImage(i_width, i_height, BufferedImage.TYPE_INT_ARGB);
  
      graphics = image.createGraphics();
      Color color = new Color(1f, 0f, 1f, 0f);
      graphics.setColor(color);
      for(int y = 0; y &lt; i_height; y++){
          for(int x = 0; x &lt; i_width; x++){
              graphics.fillRect((x), (y), 1, 1);
          }
      }
  }
  </code></pre>
  
  <p>Above, <em>image</em> is the BufferedImage I'm writing to whilst <em>graphics</em> is the Graphics2D instance. As you can probably guess, *i_width* and *i_height* represent the image's new width and height respectively.</p>
  
  <p>I determined that this method is the main source since the leaks are only noticeable when it's called.</p>
  
  <p>As you can see above, I've tried flushing the <em>image</em>, and in another section I also disposed of the <em>graphics</em>. That is as far as I managed, unfortunately, and it didn't solve the memory leak problem.</p>
  
  <p>How should I avoid this memory leak?</p>