1

So I heard from someone that it simply never releases, but what does it mean? Even if you close the program it stays in memory? How is that happening?

Couldn't find an answer for that, if this is duplicated or not good enough Q for the forum please let me know couse i really couldnt find an answer for that

Uthistran Selvaraj
  • 1,371
  • 1
  • 12
  • 31
omriman12
  • 1,644
  • 7
  • 25
  • 48
  • You're talking about garbage collection. When the program is closed, GC section removes all assigned memories. – Siyavash Hamdi Apr 27 '16 at 04:53
  • 4
    GC doesn't work with unmanaged resources. – qxg Apr 27 '16 at 04:54
  • 4
    State which resource you are concerned about. Alternatively, ask the "someone" who told you this crazy thing for a specific example. – Eric Lippert Apr 27 '16 at 04:56
  • @Eric Lippert unmanaged, nothing specific, some network sniffer in my case. i have a service, my question is should i dispose it on the service end event? – omriman12 Apr 27 '16 at 05:05
  • It never makes sense to call Dispose() on an object when its finalizer runs a millisecond later. Given that you are pretty insecure about this, make it a practice to always call Dispose(). Many .NET programmers do, they are never wrong. – Hans Passant Apr 27 '16 at 05:09
  • @HansPassant: Are you suggesting that calling dispose on all IDisposable objects when the program is terminating is useless and it should be avoided? – Luca Cremonesi May 03 '16 at 20:02
  • The contract says it is useless. Whether you can avoid it depends a lot on how much you trust the programmer of the class you use. No need to call it if Microsoft wrote it. – Hans Passant May 03 '16 at 22:06

1 Answers1

2

It means that as long as the process used to run your program lives, that resource will not be available to other processes.

This would mean that your program is hogging items which it does not need, which can cause to a degradation of the overall performance of your machine.

Once that the process dies or is killed, then the OS should make the resource available to the rest of the processes.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • I think, Each program can access to its memory which OS assigned to it. So after closing the program, all assigned and unassigned memories are removed, aren't it? – Siyavash Hamdi Apr 27 '16 at 04:59
  • @npinti so from what you say, if i have a service, so in the service end event, i do not need to call dispose on any of the unmanaged resourses because the program already ends..? – omriman12 Apr 27 '16 at 05:03
  • @SiyavashHamdi: There might be situations where you *close* your program but for some reason, the process it used to run does not die. This would mean that the resources used would still not be released. – npinti Apr 27 '16 at 05:03
  • @omriman12: You could do that, but that is by far no good practice and definitely not the way to go for production level software. You shouldn't realy entirely on the OS clearing resources for you though. – npinti Apr 27 '16 at 05:04
  • @omriman12 if the **process** ends, then yes, the operating system will dispose almost all kind of unmanaged resources it holds for it. But that's something the OS does for processes that ended unexpectedly. You should be disposing all unmanaged resources yourself on normal conditions (and unless you need them till the end of your process, release them when you have no more need for them, instead of waiting for closing) – Jcl Apr 27 '16 at 05:04
  • @npinti i dispose all unmanaged on the fly of course. but i have long running threads which last throughout the whole lifetime of the program, ex pcap sniffir, so when someone closes the service, you guys saying i should not call diapose? – omriman12 Apr 27 '16 at 05:11
  • @omriman12: What we are saying is that anything you claim must be returned back. **Thus you need to dispose** of any resource you use when you are done using it. – npinti Apr 27 '16 at 05:13