1

I have a design and I am not sure if garbage collection will occur correctly.
I have some magic apples, and some are yummy some are bad.

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

Each Basket object has a single Apple in it and each Basket stores a reference to an objectAppleSeperation.

AppleSeperation stores 2 dictionaries, YummyApples = <basketID,Apple> and BadApples = Dictionary<basketID,Apple>, so when I'm asked where an apple is I know.

An Apple object stores BasketsImIn = Dictionary<ID,Basket>, which points to the Basket and in Shops, and the Apple in Basket.

My question is, if I delete a basket from BasketList and make sure I delete the Apple from BadApples and/or YummyApples, will garbage collection happen properly, or will there be some messy references lying around?

Vort3x
  • 1,778
  • 2
  • 20
  • 37
  • 1
    Define "messy references". The rule is simple: when there are no more references to an object, the GC will eventually get rid of it. – user703016 Apr 20 '12 at 11:12
  • How about just to check it with memory profiler? – Nikolay Apr 20 '12 at 11:12
  • Why do you store an apple inside each Basket that is not Yummy nor Bad? Anyway, if the BadApples and YummyApples are only referenced from one Basket, and that Basket is no longer referenced to, then they should be collected. – Itamar Marom Apr 20 '12 at 11:13
  • @Nikolay I don't have such tools available right now, busy with a design concept brainstorming. I will once I decide on an implementation I believe should work according to good practice and C#/.Net standards. – Vort3x Apr 20 '12 at 11:16

2 Answers2

2

You are right to be thinking carefully about this; having the various references has implications not just for garbage collection, but for your application's proper functioning.

However, as long as you are careful to remove all references you have set, and you don't have any other free-standing variables holding onto the reference, the garbage collector will do its work.

The GC is actually fairly sophisticated at collecting unreferenced objects. For example, it can collect two objects that reference each other, but have no other 'living' references in the application.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • To nitpick, it will actually _not_ find "objects that are referenced, but only by objects that are themselves not reachable". What it does find is marked non-garbage. – H H Apr 20 '12 at 11:25
  • @HenkHolterman I removed my fuzzy language. My brain is still fuzzy, though; could you elaborate on your nitpick, or word it differently? I have a feeling of near-comprehension! hehe – Andrew Barber Apr 20 '12 at 11:31
  • The GC starts from a collection of 'roots' and marks everything it finds. What was not found is then considered garbage. – H H Apr 20 '12 at 11:34
  • @HenkHolterman Oh, yes! I always think about GC backwards! Replaced "finding" with "collecting" above. – Andrew Barber Apr 20 '12 at 11:36
  • 1
    @AndrewBarber: One analogy which may be helpful is to think of a bowling pinsetter after the first ball is rolled. It doesn't identify and remove individual pins that have been knocked over. It picks up all the pins it can, then sweeps up EVERYTHING, and then puts the picked-up pins back. – supercat Apr 20 '12 at 23:00
1

See http://msdn.microsoft.com/en-us/library/ee787088.aspx for fundamentals on garbage collection.

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

  1. The system has low physical memory.
  2. The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.
  3. The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

If you are performing the clean-up properly, then you need not worry!

Numan
  • 3,918
  • 4
  • 27
  • 44