Does the garbage collector work on static variables or methods in java?

后端 未结 4 1161
无人共我
无人共我 2020-12-02 00:17

I am creating a sample demo program for make me understand that how can I deallocate the reference of static variables, methods in java using garbage collector?

I a

4条回答
  •  爱一瞬间的悲伤
    2020-12-02 01:04

    You should understand that System.gc(); does not call garbage collector. It just politely asks GC to remove some garbage. GC decides what to do and when to start itself. So, do not expect that you will see any immediate effect when calling System.gc();, assigning null to variable etc.

    GC removes all objects that cannot be accessed by any way. So if code exited block where variable was defined the object can be removed. Assiging null removes the reference. Weak reference does not prevent GC from removing the object.

    I hope this explanation helps.

提交回复
热议问题