MeesterPatat
MeesterPatat

Reputation: 2811

How to remove object/instance from memory after removing it from map?

I am keeping a reference to locally created objects in a publicly available map. When i remove the object from map, it should also be removed from memory(nullified).

public class X {
    public boolean infiniteloop = true;

    public X() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (infiniteloop) {
                    System.out.println("Still exists");
                }
            }
        }).start();
    }


public class Y{
      Map myMap = new HashMap();
      public Y() throws InterruptedException{
       X x = new X();
       myMap.put("Key", x);
       Thread.sleep(10000);
       ((X)myMap.get("Key")).infiniteloop= false;
       myMap.remove("Key");
      }
 }

In the above code, "Still exists" is printend after myMap.remove("Key"). This means the object still exists in memory. How can i remove it from memory?

Upvotes: 1

Views: 5889

Answers (3)

CocoNess
CocoNess

Reputation: 4222

You are removing your object only from the Map using remove, but the object still exists. JVM will Garbage Collect your object automatically if there are no references to it. You would need to set x=null to make the X object eligible for GC. This removes the reference to your object.

Also, bear in mind that the GC process is not immediate. It is not deterministic. It runs on a separate thread in the JVM.

Setting x=null removes the reference from the original object. Then that object is eligible for GC. Also, the object has been removed from the map, so any references to the object within the map are also not there.

If your object is not getting GC'ed, make sure that there are no references to the object in the rest of the code. You provided only pseudo-code, and I think you should probably search wider for your problem

Upvotes: 2

wonderb0lt
wonderb0lt

Reputation: 2053

Java takes care of removing objects from memory at the optimal moment for you, so don't worry about that. If you really want to make sure the thread is terminated you should keep a reference to your Thread and or Runnable so you can try to shut down the Thread gracefully (and abruptly if necessary).

If you want to try to get the object removed from memory ASAP you could try System.gc() You could try System.gc() but that is considered bad practice

There's probably some trickery involved in using a parent instance variable involved that makes the thread not terminate, but someone smarter than me can surely explain that :)

Upvotes: 2

AlexW
AlexW

Reputation: 1166

The running thread does not see that infiniteloop variable has been changed. To make this change visible, you need to add some synchronization. E.g., mark this variable as volatile:

public volatile boolean infiniteloop = true;

Now it should work. No need to do anything more, no x=null and stuff. To know more, read about Java Memory Model.

Upvotes: 1

Related Questions