Annonymous Inner classes in Java and Garbage collection

"Unmesh joshi" <[email protected]> Fri, 22 Dec 2006 12:05:51 +0000
Newsgroups gmane.comp.programming.garbage-collection.general
Message-ID <[email protected]>
Hi,

Announymous inner classes in Java, particularly in the (http) session scoped 
objects in Web projects can cause problems with memory leaks. Is there any 
detailed analysis of how annonymous inner classes can cause memory leak? 
e.g. in following scenario, if the enclosing class is global scoped, can the 
annonymous inner class in the following method cause memory leak?

private Multipart newMultiPartContent(String messageText,
			    final String attachmentFileName,
			  final byte[] attachmentContent)
throws MessagingException {
	Multipart multipart = new MimeMultipart();

	MimeBodyPart messageTextPart = new MimeBodyPart();
	messageTextPart.setText(messageText, "UTF-8");
	multipart.addBodyPart(messageTextPart);

	if (attachmentContent != null) {
	    MimeBodyPart messageAttachmentPart = new MimeBodyPart();
	    DataSource source = new DataSource() {

		public OutputStream getOutputStream() throws IOException {
		    throw new UnsupportedOperationException();
		}

		public String getName() {
		    return attachmentFileName;
		}

		public InputStream getInputStream() throws IOException {
		    return new ByteArrayInputStream(attachmentContent);
		}

		public String getContentType() {
		    return "application/octet-stream";
		}

	    };
	    messageAttachmentPart.setDataHandler(new DataHandler(source));

	    messageAttachmentPart.setFileName(attachmentFileName);

	    multipart.addBodyPart(messageAttachmentPart);
	}
	return multipart;
    }

Thanks,
Unmesh

_________________________________________________________________
Get up-to-date with movies, music and TV. Its happening on MSN Entertainment 
http://content.msn.co.in/Entertainment/Default