• Post Reply Bookmark Topic Watch Topic
  • New Topic

memory leak  RSS feed

 
Dinesh Andavar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

I was wondering is there a way to create memory leaks in Java.

Does the following code create memory leak?

Class One
{
int i;
One(int i )
{
this.i=i;
}
}

Class Two
{
String name;
One o;
Two(One t , String name)
{
this.t=t;
this.name=name;
}
}

public class test{
public static void main(String args[])
{
Two t = new Two(new One(12), "Mike");
}

}

Will the One object o be in memory even after the memory is cleaned for the Two object t??

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this meant as a trick question? The code doesn't look like it would even compile, so it couldn't be causing a memory leak. And even if you made the change so that it would compile, the program terminates so quickly that the GC would most likely never be called, so memory would never be freed.
 
Dinesh Andavar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am sorry I just typed in the code...
This is the code
class One
{
int i;
One(int i )
{
this.i=i;
}
}

class Two
{
String name;
One o;
Two(One o , String name)
{
this.o=o;
this.name=name;
}
}

public class test{
public static void main(String args[])
{
someFunction();
// Lots of Line of code

}

public static someFunction()
{
Two t = new Two(new One(12), "Mike");
}

My question is when the Two Object t becomes eligible for garbage collection after someFunction() ends in main. Assume that gc is called and it clears object pointed by t. Will it clear One Object o inside the Two object t??

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, any object which isn't accessible from an active variable will be subject to garbage collection when it happens.

If you think you can just throw together some simple code which demonstrates a memory leak, forget it. The people who write Java runtime environments are 15 years ahead of you and they have taken care not only of the simple cases but also of the complicated cases. You're going to have to be pretty subtle to produce memory leaks in Java these days.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You're going to have to be pretty subtle to produce memory leaks in Java these days.


However it is much easier to commit "unintentional reference retention" than a genuine memory leak...
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
Paul Clapham wrote:You're going to have to be pretty subtle to produce memory leaks in Java these days.


However it is much easier to commit "unintentional reference retention" than a genuine memory leak...


This is actually a very good point. Some programmers think that with the GC, they don't have to worry about memory growth. And wind up doing something silly like storing temporary variables in a collection referenced by a static variable somewhere.

While technically, it isn't a memory leak, it is arguable the same. Memory that a program forgot to make unreachable is just like a memory leak.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!