I have a code like this:
Class Program { static StreamReader sr = null; static int var=0; static Program() { sr = new StreamReader("input.txt") } ~Program() { sr.Dispose(); } static void main(string args[]) { //do something with input here } }
This may not be good practice, but I just want to use this example to ask how the deconstructor and GC work.
My question is whether the Program () program will be called indefinitely or not at all in this case. If the deconstructor is not called, then how the GC collects unmanaged resources and managed resources.
He probably will not recover these resources until the AppDomain or process has been unloaded.
, . , , - Singleton. , " ", Singleton, , . WeakReference singleton.
WeakReference
, , , , ... , .
, .
dispose , ( ..). , , GC , null, , .
null
, GC . , ( ), . , "" .
, . , , , . , .
Btw GC . . IDiposable / .
# , (). , Program , ~Program() .
Program
~Program()
When (if) it is called, it only calls the method Dispose() StreamReader; because the link to StreamReaderis still preserved after the finalizer is called, the instance will never be garbage collected before the program exits.
Dispose()
StreamReader
Source: https://habr.com/ru/post/1739358/More articles:файл конфигурации с разделенным единством - unity-containerCommunication between .NET programs - c #jquery ui slider with a hint? - jqueryChecking -moz-border-radius / -webkit-border-radius - cssChoosing an MVC Viewer - asp.net-mvcC ++ Universal Data Type - c ++How to upgrade an existing EDMX 1.0 model to a new EDMX 2.0? - .net-4.0Rabin fingerprint using .NET - .netJSONP Long polling always loading - cometLinq to Entities Convert DateTime - datetimeAll Articles