tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robert Hunt" <robert-h...@comcast.net>
Subject [Slightly OT] coercing AWT/image libraries to release resources
Date Sat, 23 Oct 2004 13:35:01 GMT
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: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message