1
2
Jan 19 2014, 9:55 am
In response to Stephen001
|
|
Which is what?
|
Well, ordinarily, if your code is in such a situation where you can add object references, but have no sensible context with which to remove them again from the thing you added them to, you probably have spaghetti code, and very odd divisions of responsibility within the codebase.
Usually whatever passes a reference into an object, should be able to fetch/use that object again to remove said reference later. |
Usually whatever passes a reference into an object, should be able to fetch/use that object again to remove said reference later. Moreover, you need to break circular references and only maintain references when they are needed. Eric.partymembers = list(Tom, Jeff, Tim) In the above example, none of the above objects can be deleted until all of them are deleted, or you explicitly delete one. So, in order to ensure that we aren't keeping our friends around when we don't need to, we can do something like the below: mob Even better, we could abstract this behavior, and only keep a singular reference to the entire party: party In this scenario, until the circular references are cleaned up, neither the party members, nor the party itself can be removed by the garbage collector. Usually, it's better to recycle objects rather than letting the garbage collector have them, but it's just better to design your systems in such a way that they will maintain themselves and the garbage collector won't have to operate too hard, and you won't have to tell it what things need deleting. |
1
2