As we know for Object its works through Reference counting and other such Algorithm.
But for Primitive Data types, we can't make it NULL
like:
int a = NULL;
How does Garbage Collector work for Primitive datatypes in Java?
As we know for Object its works through Reference counting and other such Algorithm.
But for Primitive Data types, we can't make it NULL
like:
int a = NULL;
How does Garbage Collector work for Primitive datatypes in Java?
Primitive data types are either fields in objects or used in arrays which themselves are objects. For the garbage collector these fields are not relevant, because they do not contain pointers. They can be perfectly ignored and will be freed together with the object/array once it gets garbage collected.
Primitives go on
stack
and freed immediately when they go out of scope, there is no GC for it. As oppose to objects that go toheap
and stored in more long term memory.