简体   繁体   English

如何确保这些对象被垃圾收集?

[英]How do I make sure these objects gets garbage collected?

I am currently writing a small server for an online game. 我目前正在为在线游戏编写一个小型服务器。 It creates one "Server" object which handels connections and data transfer, and one "Game" object per created game session between two players. 它创建了一个“服务器”对象,它处理连接和数据传输,并在两个玩家之间每创建一个游戏会话创建一个“游戏”对象。

The "Game" class extends "Thread", having an infinite loop in it's run() method. “Game”类扩展了“Thread”,在它的run()方法中有一个无限循环。

Each "Game" object holds references to several other objects (the pieces on the board, so to say), and these objects themselves hold a reference to the corresponding "Game" object, because they need to communicate with each other on a regular basis. 每个“游戏”对象都包含对其他几个对象(板上的棋子,也就是说)的引用,并且这些对象本身包含对相应“游戏”对象的引用,因为它们需要定期相互通信。

Now, when a game has ended, and the "Game" object is no longer needed - what steps do I have to take to make sure that the "Game" object gets garbage collected? 现在,当游戏结束并且不再需要“游戏”对象时 - 我必须采取哪些步骤来确保“游戏”对象被垃圾收集?

My idea would be: 我的想法是:

In the "Server" object: remove reference to the Game object in question. 在“服务器”对象中:删除对相关游戏对象的引用。

In every object used by the Game object in question: remove (nullify?) reference to the Game object in question. 在所讨论的Game对象使用的每个对象中:删除(nullify?)对相关Game对象的引用。

Break the infinite loop that is running the Game object's run() method. 打破运行Game对象的run()方法的无限循环。

Would this suffice, or are there other steps neccesary, or are some of these steps not neccesary? 这是否足够,或者是否有必要的其他步骤,或者这些步骤中的某些步骤是不必要的?

This will suffice: 这就足够了:

In the "Server" object: remove reference to the Game object in question. 在“服务器”对象中:删除对相关游戏对象的引用。

If the game is not referenced from the root of your application, then it can be garbage collected, and all objects that it referenced as well (doesn't matter that they referenced the game back, since this whole graph of objects still isn't reachable from anywhere). 如果游戏没有从你的应用程序的根目录中引用,那么它可以被垃圾收集,并且它所引用的所有对象也是如此(无论他们引用游戏回来都没关系,因为整个对象图形仍然不是可以从任何地方到达)。

All you need to do is to remove the reference to the Game object in the Server . 您需要做的就是删除ServerGame对象的引用。 Even though Game has references to other objects and those objects to Game (forming cycles/circular references), they are still not reachable from the GC root and so will end up being garbage collected anyway. 尽管Game引用了其他对象和那些对象到Game (形成循环/循环引用),但它们仍然无法从GC根目录到达,因此无论如何最终都会被垃圾收集。

Now, when a game has ended, and the "Game" object is no longer needed - what steps do I have to take to make sure that the "Game" object gets garbage collected? 现在,当游戏结束并且不再需要“游戏”对象时 - 我必须采取哪些步骤来确保“游戏”对象被垃圾收集?

So if your application really does not have any references to the objects in question, the the garbage collector will free them. 因此,如果您的应用程序确实没有对相关对象的任何引用,则垃圾收集器将释放它们。 The Game thread should finish (ie the run() method should return) so the Thread will be reaped. Game线程应该完成(即run()方法应该返回)所以Thread将被收获。 Then any objects that are help by the Game object will then in turn have their reference counters decremented allowing them to be freed. 然后, Game对象帮助的任何对象将依次使其引用计数器递减,从而允许它们被释放。

That said, once and a while if you have a large amount of object bandwidth, the GC can be helped by setting fields in your Game object to be null to specifically remove the references in ownership objects. 也就是说,如果你有大量的对象带宽,可以通过将Game对象中的字段设置为null来专门删除所有权对象中的引用来帮助GC。 This is especially true, for example, with Android applications which are notoriously sensitive to GC periods since many old mobile devices are single core. 例如,对于因GC周期而言非常敏感的Android应用程序尤其如此,因为许多旧的移动设备都是单核心。

However, the only time you should need to specifically set the fields to be null is if you suspect that the garbage collector is falling behind and unable to reap the objects in a timely manner. 但是,如果您怀疑垃圾收集器落后并且无法及时收到对象,则唯一需要将字段专门设置为null的时间。 This is somewhat rare. 这有点罕见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM