Search code examples
javamultithreadinggarbage-collectiongarbage

how can i change the code so the garbage collector will delete this instance due the program?


In the line i marked with //D, there is a one-time use with the object instance Scanner. but its memory witll stay in the heap as long as the program plays(which is forever). why the garbage collector wont delete this instance object? how can i change the code so the garbage collector will delete this instance due the program? thanks

package Try;

import java.util.Random;
import java.util.Scanner;

public class Foo1 extends Thread {

    private int min_, max_;
    Foo1(int max, Integer min) {

    max_ = max;
    min_ = min.intValue();
    }

    public void run() {

        Random rand_gen = new Random();
        while(true) {
            try {
                Thread.sleep(rand_gen.nextInt(max_-min_) + min_);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("you got new message");
        }
    }

    public static void main(String[] args){

        System.out.println("Insert 1 to start"); 

        Scanner sc = new Scanner(System.in); // D

        int i = sc.nextInt();

        if (i == 1) {
            Foo1 f1;
            int max = 1000;
            Integer min = new Integer(1000);
            Foo1 f2 = new Foo1(max, min);
            f1 = f2; // A
            f1.start();
        }
    }
}

Solution

  • You can delete the reference to the scanner by setting it to null i.e. sc = null.

    -OR-

    If you do not need Scanner anymore, you can close it after using it:

    int i = sc.nextInt();
    sc.close();
    

    Even better is to use try-with-resources as follows:

    int i;
    try (Scanner sc = new Scanner(System.in)) {
        i = sc.nextInt();
    }
    

    Note that sc.close() closes the scanner and releases the resource while sc = null deletes the reference to the scanner but the resource may still remain open.

    Warning: Never close the Scanner for System.in if you still need it for more inputs from System.in as closing the Scanner will also close System.in. Consider the following code:

    String s;
    System.out.print("Enter a text: ");
    try (Scanner sc = new Scanner(System.in)) {
        s = sc.nextLine();
    }
    
    try (Scanner in = new Scanner(System.in)) {
        System.out.print("Enter another text: ");
        s = in.nextLine();
    }
    

    Trying to execute the code given above will result in:

    Enter a text: hello
    Enter another text: Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
        at Main.main(Main.java:14)