Re: Annonymous Inner classes in Java and Garbage collection

"Unmesh joshi" <[email protected]> Sat, 23 Dec 2006 01:38:08 +0000
Newsgroups gmane.comp.programming.garbage-collection.general
Message-ID <[email protected]>
Hi,

In case annonymous inner classes are refering to final local variable as in 
following example. If the enclosing intance is kept in http session and this 
method is called multiple times on the instance. Each time a new annonymous 
class instance will be created refering to final byte[], attachmentContent. 
If enclosing class somehow keeps reference to annonymous instance, there can 
be huge unreclaimed memory and might cause session memory to overflow, isnt 
it?

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

	    };


Thanks,
Unmesh

>From: Eliot Moss <[email protected]>
>Reply-To: [email protected]
>To: "Unmesh joshi" <[email protected]>
>CC: [email protected]
>Subject: [gclist] Annonymous Inner classes in Java and Garbage collection
>Date: Fri, 22 Dec 2006 10:01:46 -0500


>
>Unmesh -- What memory do you think is being "leaked", i.e., not reclaimed?
>Are you concerned about failure to reclaim a _class_ or an _instance_ (and
>of what class)? I don't see that anonymous inner classes pose any
>particular new problem. Note that while, in _concept_, you get a new
>anonymous class for each enclosing _instance_, in practice the class exists
>just once, with no statics (by design of the language feature), and an
>instance refers to an instance of the outer class. Does this help answer
>the question?
>
>-- Eliot Moss
>==============================================================================
>J. Eliot B. Moss, Associate Professor     http://www.cs.umass.edu/~moss    
>www
>Director, Arch. and Lang. Impl. Lab.      +1-413-545-4206                
>voice
>Department of Computer Science            +1-413-695-4226                 
>cell
>140 Governor's Drive, Room 372            +1-413-545-1249                  
>fax
>University of Massachusetts at Amherst    [email protected]              
>email
>Amherst, MA  01003-9264  USA              +1-413-545-3733 Priscilla Coe  
>sec'y
>==============================================================================
>

_________________________________________________________________
Want to know what your favourite stars are up to? Catch the latest gossip in 
Bollywood on MSN Entertainment! 
http://content.msn.co.in/Entertainment/Default