Java lets you interact politely with the Garbage Collector using four reference strengths—and gives you java.lang.ref.Cleaner (Java 9+) as the safe safety-net backup to AutoCloseable instead of dangerously broken finalize().
More lessons in Complete Core Java