This site requires JavaScript, please enable it in your browser!
Greenfoot back
Dalvengyr
Dalvengyr wrote ...

2014/11/25

Memory leak

Dalvengyr Dalvengyr

2014/11/25

#
I'm used to believe that local handle may causes memory leaks. Memory leaks is unaccessable process, that process remains there and we have no way to stop it. It occurs mostly in local variables. I will just give one example case: In act() method, I will create a list containing some actor:
List<Actress> list = getObjectsInRange(500, Actress.class);
Then I never call this:
list.clear();
I'm afraid the list will remain there forever if I don't clear (destroy) the list, that list will obviously become a burden for your memory. So the question is, will the local list get cleaned automatically after being used? Since in the next "act" we are unable to access that previous list. So we keep declaring list filled with actresses over and over again, logically, it will increase the consumed memory right? Hope you get my question :) Thanks for any response.
davmac davmac

2014/11/25

#
I'm afraid the list will remain there forever if I don't clear (destroy) the list, that list will obviously become a burden for your memory.
As it's referenced in a local variable, the reference is destroyed as soon as the method returns and the object then becomes valid for reclamation by the garbage collection process (as long as it isn't referenced anywhere else). This process will also reclaim the items in the list. There is no need to clear the list manually. Some other programming languages/environments do not have automatic memory management, and there you would need to manage this yourself, but in Java it is not normally an issue.
Dalvengyr Dalvengyr

2014/11/25

#
Glad to hear. But how bout class members? E.g. I have non-static members
private List<Something> list;
private int X;
private int Y;

void act()
{
    getWorld().removeObject(this);
}

public Actress(int x, int y)
{
    list = getObjectsInRange(500, Something.class); 
    X = x;
    Y = y;
}
The class name is Actress. What happen to the list and other members if I never clear them before I remove the Actress from the world? Will they stay there forever without being destroyed automatically? Thanks for your response anyway :)
davmac davmac

2014/11/25

#
Look at it this way. If the actor is removed from the world (and you don't have a reference to it stored elsewhere), then it is no longer referenced - or "reachable" - and at that point anything that it turn references, such as your non-static members, are also unreachable (again, unless they are reachable via some other reference). Anything that isn't reachable can be reclaimed by garbage collection. There is no problem.
Dalvengyr Dalvengyr

2014/11/25

#
davmac wrote...
Anything that isn't reachable can be reclaimed by garbage collection
So the conclusion is that I don't need to clear the list before removing an actor right? That's the whole question actually :3
davmac davmac

2014/11/25

#
davmac wrote...
There is no need to clear the list manually.
I think this is as clear as I can be ;)
Dalvengyr Dalvengyr

2014/11/25

#
Right. Thanks for the information :)
You need to login to post a reply.