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

2021/10/24

garbage collection

1
2
PetroGR PetroGR

2021/10/24

#
Why is the garbage collector not working in Greenfoot? There is no longer a reference to the object, but it still continues to live and is not removed by the garbage collector. By default (by default) in Greenfoot, objects are created in the prepare () method and, according to the rules of the Java language, references are lost at the end of the method and the objects must be removed by the garbage collector. But they live fine for themselves. What's the focus?
danpost danpost

2021/10/24

#
PetroGR wrote...
There is no longer a reference to the object
Are you sure?
PetroGR wrote...
By default (by default) in Greenfoot, objects are created in the prepare () method and, according to the rules of the Java language, references are lost at the end of the method and the objects must be removed by the garbage collector
Show method. Indicate which object remain.
Super_Hippo Super_Hippo

2021/10/24

#
A world has a list of all actors inside of it. When you add an actor to a world with the ‘addObject’ method, the actor is added to that list. Exiting the prepare method loses the reference to variables you created there (see below), but the actors don’t remove themselves.
private void prepare()
{
    Actor a = new Wombat();
    addObject(a, 10, 15);
}

public void act()
{
    //this doesn’t work because ‘a’ only exist in the ‘prepare’ method
    if (a.getX() > 20)
    {
        a.setLocation(20, a.getY());
    }
    
    //this gets the first Wombat actor in the world’s actor list
    Actor a = getObjects(Wombat.class).get(0);
    if (a.getX() > 20)
    {
        a.setLocation(20, a.getY());
    }
}
PetroGR PetroGR

2021/10/25

#
So that's the question. If the reference is no longer there, then the garbage collector should also delete the object, but this does not happen. Why?
PetroGR PetroGR

2021/10/25

#
There are two options. The first has a persistent link declared as a field. But in the second version, the reference is like a local variable. That is, it is valid until the end of the method execution. And then the link is lost. This means that the garbage collector must clean up such objects that do not have a reference. first: import greenfoot.*; public class CrabWorld extends World { Crab crab; public CrabWorld() { super(560, 560, 1); prepare(); } private void prepare() { crab = new Crab(); addObject(crab,232,258); } } second: import greenfoot.*; public class CrabWorld extends World { public CrabWorld() { super(560, 560, 1); prepare(); } private void prepare() { Crab crab = new Crab(); addObject(crab,232,258); } }
PetroGR PetroGR

2021/10/25

#
The addObject method declares a reference to the created object when it is added to the World? How do you find out this name of the object?
danpost danpost

2021/10/25

#
PetroGR wrote...
The addObject method declares a reference to the created object when it is added to the World? How do you find out this name of the object?
When you execute the addObject(Actor, int, int) method, you pass the reference of the actor (crab) to the World object, which it then saves in a List object. The getObjects(Class) method returns a sub-list of that List object containing all of the actors of the class requested that are currently in the world. So:
addObject(new Crab(), 0, 0); // no reference to actor here in method
Crab crab = (Crab) getObjects(Crab.class).get(0); // acquiring a reference to crab
crab.setLocation(232, 258); // using the reference
The world will retain that reference in the list until the actor is removed from the world, at which point, if no more references to that actor exists, the garbage collector will do its work.
PetroGR PetroGR

2021/10/25

#
Thank you, I suspected that, but there is no such explanation in the manual. And what will happen in the first option? After all, a link will be created like a field, and then a link to the same object will be created again in the prepare () method using the addObject method. With what link does the act () method of the Crab class wake up?
PetroGR PetroGR

2021/10/25

#
I think this approach is very confusing for beginners to learn Java. They read that if there is no reference, the object should be deleted, but it turns out that the reference was implicitly created by the addObject method. It may be better for them to teach them to create links using the field when the object must live for a long time. And so, for example, I heard such a version that Greenfoot takes up very little memory space and just the garbage collector does not clean up objects without references ... Maybe this pleasant surprise should be removed from the addObject method?
danpost danpost

2021/10/25

#
PetroGR wrote...
With what link does the act () method of the Crab class wake up?
Once the actor is added into the world -- or, rather, once the actor is referenced within the world's list (of actors contained in that world), then the act method for that actor is called each frame of the simulation. Without the reference to the actor in the world's list, the act method would not be able to be called on that actor (by greenfoot).
PetroGR PetroGR

2021/10/26

#
Thanks, but I still have a question. When we create a link in this way import greenfoot. *; public class CrabWorld extends World { Crab crab = new Crab (); it is not added to the List. After all, only the addObject method adds a link to the List. And then if we delete the object that we added to the World using the addObject method in the act () method, anyway, the reference to the object that we created as a field will exist, then the object will exist. This is true?
danpost danpost

2021/10/27

#
PetroGR wrote...
When we create a link in this way import greenfoot. *; public class CrabWorld extends World { Crab crab = new Crab (); it is not added to the List. After all, only the addObject method adds a link to the List.
Correct.
And then if we delete the object that we added to the World using the addObject method in the act () method
(could be anywhere the world is accessible)
the reference to the object that we created as a field will exist, then the object will exist. This is true?
This is true. Setting the field to null would lose the actor to the garbage collector.
PetroGR PetroGR

2021/10/27

#
Many thanks. That is, in this case, we got two references to the same object. And to remove the object completely, the removeObject () method is not enough for us, we must also assign the value null to the crab field. Did I understand you correctly?
PetroGR PetroGR

2021/10/27

#
Therefore, the Greenfoot developer automatically adds an object using the prepare () method so as not to monitor the field value yet? But very often it is more convenient to have a specific name for the link, rather than calculate it using getObjects (). Can it still be more correct to teach children to immediately create a link as a field, if we use the object for a long time? At the same time, clarifying that when adding an object to the world using addObject, we create a second reference to this object. And if you do not need a long-lived object, then you can create it in the method using addObject. Your opinion?
PetroGR PetroGR

2021/10/27

#
My opinion. That is, I am opposed to the prepare () method being automatically created with the creation of an object in it, when I drag the created object into the World and write it down. I would automatically declare the reference field, and then add only the addObject () method in the prepare () method (or better in the constructor).
There are more replies on the next page.
1
2