Primitive types in Java are allocated on the stack, so their memory is automatically freed when they go out of scope. Object references are primitives that are similarly managed, but the objects themselves collect garbage. They will be automatically deleted by the garbage collector, but it is not guaranteed how long it will take.
The JVM garbage collector starts automatically when the pressure in the memory becomes hard, so as long as there is no reference to the object, you can effectively make the assumption that its memory will be freed.
source
share