How does Garbage Collector works for Primitive Dat

2019-05-29 03:17发布

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?

2条回答
放荡不羁爱自由
2楼-- · 2019-05-29 03:30

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.

查看更多
神经病院院长
3楼-- · 2019-05-29 03:43

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.

查看更多
登录 后发表回答