Re: Annonymous Inner classes in Java and Garbage collection
"Unmesh joshi" <[email protected]> Sun, 24 Dec 2006 03:43:17 +0000
| Newsgroups | gmane.comp.programming.garbage-collection.general |
|---|---|
| Message-ID | <[email protected]> |
hi, Sorry for vaguesness in the question I asked. It is just that annonimous inner classes can keep object references which are not so obvious to see just by looking at the code. References to final local variables is one way of holding the reference which can be easily missed. I wanted to know if annonymous inner classes, the way they are impleneted in Java, can cause any issues which are harder to figure out unless you know inner working of JVM. Thanks, Unmesh >From: Maria Jump <[email protected]> >To: Unmesh joshi <[email protected]> >CC: [email protected], [email protected] >Subject: Re: [gclist] Annonymous Inner classes in Java and Garbage >collection >Date: Sat, 23 Dec 2006 21:26:48 -0600 (CST) > > >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; >> } _________________________________________________________________ Get up-to-date with movies, music and TV. Its happening on MSN Entertainment http://content.msn.co.in/Entertainment/Default