Connecting Tech Pros Worldwide Help | Site Map

Memory leak problem

sajithamol
Guest
 
Posts: n/a
#1: Jul 5 '07
What is a memory leak problem?can anyone explain it with eg
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jul 5 '07

re: Memory leak problem


Quote:

Originally Posted by sajithamol

What is a memory leak problem?can anyone explain it with eg

For theoretical type questions, you are best to consult a text.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Jul 5 '07

re: Memory leak problem


Quote:

Originally Posted by sajithamol

What is a memory leak problem?can anyone explain it with eg

Using Java it's quite difficult to leak memory; here's an obvious example:

Expand|Select|Wrap|Line Numbers
  1. class Foo {
  2.    private static List<Foo> leak= new ArrayList<Foo>();
  3.    public Foo() {
  4.       leak.add(this);
  5.    }
  6. }
  7.  
Now keep on allocating Foo objects and keep forgetting about them. The garbage
collector is very willing to reap them but it can't: the 'leak' list still refers to them all.


kind regards,

Jos
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jul 6 '07

re: Memory leak problem


Quote:

Originally Posted by JosAH

Using Java it's quite difficult to leak memory; here's an obvious example:

Expand|Select|Wrap|Line Numbers
  1. class Foo {
  2.    private static List<Foo> leak= new ArrayList<Foo>();
  3.    public Foo() {
  4.       leak.add(this);
  5.    }
  6. }
  7.  
Now keep on allocating Foo objects and keep forgetting about them. The garbage
collector is very willing to reap them but it can't: the 'leak' list still refers to them all.


kind regards,

Jos

Ha, your examples leave a lot to be desired.
So the Foo objects will only become eligible for collection when the Foo class is unloaded from the JVM?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Jul 6 '07

re: Memory leak problem


Quote:

Originally Posted by r035198x

Ha, your examples leave a lot to be desired.
So the Foo objects will only become eligible for collection when the Foo class is unloaded from the JVM?

Yep, the Foo objects will stay there until the class itself is GC'd.

kind regards,

Jos
Reply