Hi, A profiler would show you what references are being kept, so that might be helpful in knowing what to flush (assuming it's possible at all -- not a trivial assumption with AWT). In any case, so what if the ImageIcon takes 30MB?
Yoav Shapira http://www.yoavshapira.com >-----Original Message----- >From: Robert Hunt [mailto:[EMAIL PROTECTED] >Sent: Saturday, October 23, 2004 9:35 AM >To: [EMAIL PROTECTED] >Subject: [Slightly OT] coercing AWT/image libraries to release resources > >I've got a servlet that produces thumbnail images on-the-fly (using TC >5.0.25, JDK 1.4.2). Once the servlet becomes active, it gobbles about 30mb >per servlet/instance. I've gone through and tried to apply as many >flush()/dispose()/gc()/etc. methods as I could find, but still, the memory >remains allocated. Has anyone found a way to coerce the AWT (or >appropriate >Java imaging class) into releasing the memory? > > > >The code (from doGet( request, response) method): >==================================================== > ImageIcon imgIcon = null; > Image img = null; > try { > imgIcon = new ImageIcon( jpegFile ); // this is the point at >which memory swells to like 30mb. > } > catch ( Exception e ) > { > e.printStackTrace(); > response.sendError( HttpServletResponse.SC_NOT_FOUND, >filename ); > } > img = imgIcon.getImage(); > imgIcon = null; > > > // Determine the scale. > double scale = Math.min( (double) h / (double) img.getHeight( null >) > , (double) w / (double) img.getWidth( null ) > ); > > > // Determine size of new image. > int scaledW = (int)( scale * img.getWidth( null ) ); > int scaledH = (int)( scale * img.getHeight( null ) ); > > BufferedImage thumb = new BufferedImage( scaledW, scaledH, >BufferedImage.TYPE_INT_RGB ); > > // Set the scale. > AffineTransform tx = new AffineTransform(); > if ( scale < 1.0d ) tx.scale( scale, scale ); > > // Paint image. > Graphics2D g2d = thumb.createGraphics(); > g2d.drawImage( img, tx, null ); > g2d.dispose(); > g2d = null; > > img.flush(); > img = null; > > OutputStream out = response.getOutputStream(); > ImageIO.write( thumb, ext, out ); > > thumb.flush(); > thumb = null; >==================================================== > > > >--------------------------------------------------------------------- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]