Re: Annonymous Inner classes in Java and Garbage collection

Maria Jump <[email protected]> Sat, 23 Dec 2006 21:26:48 -0600 (CST)
Newsgroups gmane.comp.programming.garbage-collection.general
Message-ID <[email protected]>
Hi there,
So I don't know if I'm mis-understanding the question.  Looking at the 
code snippet that you attached with the definiton of the inner class I'm 
not sure what you mean when you say that the inner class could "cause" a 
memory leak.  It is possible that instances of the inner class could be 
filling up your memory depending upon what you do with the Multipart 
object that you return, but the inner class itself has no data members and 
therefore will not have a handle to the byte array that you referred to.

In Java the "cause" of a memory leak is usually some data structure which 
grows without bound.  Nothing that I see here would qualify it as the 
cause.

Or am I totally misunderstanding the question ...

As far as whether there is any analysis done, every memory leak detection 
tool for Java that I am familiar with (including my own, Cork) does not 
discriminate as to whether a class is an inner class or not (inner classes 
like this one would show up with a name like: ParentClass$DataSource or 
even ParentClass$1 for a truly annonymous class.

-Maria

> 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;
>   }