4

Lately I had a question on an interview about Zombie Objects in c#. Could you kindly explain to me by using a simple example what they are ?

The zombie objects in that interview were related to applications regarding Win-forms, can we get these objects in asp.net MVC for example ?

Thank you for taking the time to explain, because I have searched for it and i did not find a example or an explanation that I could understand

Wolf
  • 170
  • 1
  • 4
  • 19
Rafal_Koscinski
  • 317
  • 5
  • 22
  • 2
    Here's a [Similar Question](http://stackoverflow.com/questions/3220676/what-are-zombies-and-what-causes-them-are-there-zombie-processes-and-zombie-obj) – Jonesopolis Feb 03 '14 at 14:26
  • I would imagine they were referring to the event handler memory leak problem? – Oblivious Sage Feb 03 '14 at 14:41
  • If you can get zombie objects in .net, you can get them in any .Net application, forms, asp, wpf, everywhere. Because they would be related to framework, not a specific language – T.S. Feb 03 '14 at 14:46
  • @Jonesy Zombie processes are something different than what was asked, although I never heard of zombies in a .net context. – thumbmunkeys Feb 03 '14 at 14:55
  • there is definitely discussion of zombie objects in that question – Jonesopolis Feb 03 '14 at 14:58
  • 4
    Zombies are un-dead creatures who intend to eat people's brains (and plants). You can eliminate your zombies problem by putting a bunch of pea shooting plants in your garden / backyard. – Federico Berasategui Feb 03 '14 at 15:00
  • @Jonesy Cocoa is some iOS technology if I'm not mistaken – thumbmunkeys Feb 03 '14 at 15:09
  • 1
    Related: [Do zombies exist...in.NET?](http://stackoverflow.com/questions/20065780/do-zombies-exist-in-net) This has some excellent information about Zombie *threads* in .NET, what causes them, and how to avoid them. It does not mention zombie *objects*, though. Are you sure that's what the interview question said? – Josh Darnell Feb 03 '14 at 15:17
  • A "zombie" anything in programming is simply something that was created and never disposed of. It could be a thread, and process, an instance of a class, etc. However, it's generally only relevant in modern programming to things like threads or processes which lead to true memory leaks. All modern CLRs have *some* form of garbage collection that will clean up instantiated classes and the like. – Chris Pratt Feb 03 '14 at 16:54
  • Thank you everyone and sorry for duplicating, i've read the one that I've duplicated but your explanation fit's my needs, that one did not @jadarnel27: yes I'm sure that was the question – Rafal_Koscinski Feb 04 '14 at 10:36

1 Answers1

4

The event-caused zombie situation is this:

class AnObjectThatWillSoonGoOutOfScope{
  public AnObjectThatWillSoonGoOutOfScope(ISomeLongLivedService service){
    service.SomeEvent += OnSomeEvent;
  }
  private void OnSomeEvent(...){...}
}

The long-time service keeps a reference to the child object that should have unsubscribed from the event before going out of scope. You can use a dispose pattern to avoid this scenario. You can use a tool like the Ants Memory Profiler to track these down. Generally the problem does not exist if you are subscribing to events on your own instance (because both the subscribee and subscriber will be available for garbage collection).

Brannon
  • 5,324
  • 4
  • 35
  • 83
  • And if your class is a WPF Control, you can use the weak event manager to help with this scenario. – Brannon Jan 21 '16 at 16:31