Solved

inner class memory leaks

Posted on 2013-01-24
2
552 Views
Last Modified: 2013-01-25
hi all
<<<Non static inner classes can result in memory leaks while static inner class will protect against them. If the outer class holds considerable data, it can lower the performance of the application. >>

Is the above statement true? Can someone please explain with example?

thanks
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
tvedtem earned 500 total points
ID: 38816850
The reason for this statement is that such classes hold an implicit reference to their parent.

This doesn't necessarily cause a memory leak.

What it means is that if you create an instance of a non-static inner class, an instance of the enclosing class will also be created.

It follows that if you create an instance of an inner class from somewhere else, you are effectively keeping a reference the enclosing class, for as long as the inner class is alive.

If the enclosing class contains something large, you can 'leak' a large amount of memory without realising (since you don't realise you are keeping an instance of the enclosing class alive alongside the inner class).

In practice all you really need to do is be careful when constructing an instance of an inner class.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question