Re: Annonymous Inner classes in Java and Garbage collection
"Unmesh joshi" <[email protected]> Sun, 24 Dec 2006 00:28:39 +0000
| Newsgroups | gmane.comp.programming.garbage-collection.general |
|---|---|
| Message-ID | <[email protected]> |
Well, thats true. Unless there is explicit reference to inner instance, it will be GCed. Only thing is that you have to be little careful using inner classes, particularly if you are keeping instances in http sesssion. Thanks, Unmesh >From: <[email protected]> >Reply-To: [email protected] >To: Unmesh joshi <[email protected]>, [email protected] >CC: [email protected] >Subject: Re: [gclist] Annonymous Inner classes in Java and Garbage >collection >Date: Sat, 23 Dec 2006 13:48:54 -0800 (PST) > >Sorry for the intrusion, but I think there is a slight mistake in that. It >is the inner instance that keeps an implicit reference to the enclosing >instance, not the other way round. If you drop references to the inner >instance, it will be GCed. What could happen is that if you keep a >reference to the inner instance and drop all references to the enclosing >instance then the enclosing instance will not be GCed because it is >referred to by the inner instance(s). > >Hope that helps. >-- Douglas > >Unmesh joshi <[email protected]> wrote: 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 > >Reply-To: [email protected] > >To: "Unmesh joshi" > >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 > > > > _________________________________________________________________ Spice up your IM conversations. New, colorful and animated emoticons. Get chatting! http://server1.msn.co.in/SP05/emoticons/