When is the WeakReference object itself recycled? (instead of constructing objects passed in by weak references)

Car car = new Car(22000,"silver");
WeakReference<Car> weakCar = new WeakReference<Car>(car);
car  = null;
System.gc();

when the above code is gc, weakCar.get () returns null, indicating that the Car object has been recycled, but the question is, when will the weakCar object be recycled? Isn"t it a bit wasteful to recycle a car object and new another weakCar object?

Jul.07,2021

weakCar objects are recycled at the same time as ordinary objects, that is, when gc root is unreachable,
the weak reference itself is not large, but the referenced object may not be small, so reasonable use is not a waste

Menu