How to prevent an object from getting garbage collected?

后端 未结 11 1958
太阳男子
太阳男子 2020-12-02 13:17

How to prevent an object from getting garbage collected?

Are there any approaches by finalize or phantom reference or any other approaches?

I was asked this

11条回答
  •  忘掉有多难
    2020-12-02 13:47

    If there is still a reference to the object, it won't get garbage collected. If there aren't any references to it, you shouldn't care.

    In other words - the garbage collector only collects garbage. Let it do its job.

提交回复
热议问题