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();
}
}
}