C # garbage collection and links

I have a design, and I'm not sure if garbage collection will occur correctly.
I have some magic apples and some are tasty, some of them are bad.

I have a dictionary: BasketList = Dictionary <basketID,Basket>
(list of baskets).

Each Basket object has one Apple in it, and each Basket stores a reference to the AppleSeperation object.

AppleSeperation stores 2 dictionaries, YummyApples = <basketID,Apple> and BadApples = Dictionary<basketID,Apple> , so when they ask me where I know, I know.

The Apple object stores BasketsImIn = Dictionary<ID,Basket> , which points to the basket both in stores and also to Apple in the basket.

My question is: if I delete the basket from BasketList and make sure that I remove Apple from BadApples and / or YummyApples , will the garbage collection properly, or will there be any dirty links?

+1
garbage-collection collections c #
source share
2 answers

You are right to think it over carefully; having various links has consequences not only for garbage collection, but also for the proper functioning of your application.

However, if you carefully delete all the links you specify and you do not have other independent variables holding the link, the garbage collector will do its job.

GC is actually quite complicated when collecting objects without references. For example, it can collect two objects that refer to each other, but do not have other "live" links in the application.

+2
source share

See http://msdn.microsoft.com/en-us/library/ee787088.aspx for the basics of garbage collection.

From the above link, when garbage collection occurs ...

  • The system has low physical memory.
  • The memory used by the allocated objects in the managed heap exceeds the allowable threshold. This means that the allowed heap is exceeded on the managed heap. This threshold is continuously adjusted as the process starts.
  • The GC.Collect method is called. In almost all cases, you do not need to call this method because the garbage collector runs continuously. This method is mainly used for unique situations and testing.

If you do the cleaning properly, you don’t have to worry!

+1
source share

All Articles