What is a memory leak problem?can anyone explain it with eg
Memory leak problem
Collapse
X
-
sajithamolTags: None -
Using Java it's quite difficult to leak memory; here's an obvious example:Originally posted by sajithamolWhat is a memory leak problem?can anyone explain it with eg
[code=java]
class Foo {
private static List<Foo> leak= new ArrayList<Foo>( );
public Foo() {
leak.add(this);
}
}
[/code]
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,
JosComment
-
Ha, your examples leave a lot to be desired.Originally posted by JosAHUsing Java it's quite difficult to leak memory; here's an obvious example:
[code=java]
class Foo {
private static List<Foo> leak= new ArrayList<Foo>( );
public Foo() {
leak.add(this);
}
}
[/code]
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
So the Foo objects will only become eligible for collection when the Foo class is unloaded from the JVM?Comment
-
Yep, the Foo objects will stay there until the class itself is GC'd.Originally posted by r035198xHa, 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?
kind regards,
JosComment
Comment