Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Garbage Collector works for Primitive Data type 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?

like image 674
Jigar Avatar asked Jul 19 '26 04:07

Jigar


2 Answers

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.

like image 183
smarr Avatar answered Jul 21 '26 18:07

smarr


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 to heap and stored in more long term memory.

like image 39
Konstantin Avatar answered Jul 21 '26 17:07

Konstantin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!