Tell me how to work with the garbage collector? There was never a need to independently control when an object was physically removed, but a problem arose.
The problem emerged from the other: an error (cannot start thread) .
The essence of the problem is that in the cycle connections to the database are opened and closed (to different databases, about several thousand databases are being searched). As it turned out, although the object is created in the using() {}
block, even when exiting this block, the connection to the database remains hanging (at the level of the firebird server). All connections will instantly break as soon as the application is closed. I have only one option left: the object, although it closed the connection and the object.Dispose()
method was automatically object.Dispose()
upon exiting the using
block, the object still hangs in memory, and thus object.Dispose()
server from breaking the connection to the database. At the support forum .net of the firebird provider, I was told that I had to learn how to physically nail objects, and not rely on garbage collectors. Here I sit now and wonder how to use this hint. The manual did not understand how to use GC.*
. More precisely, I tried various options, but the problem did not dare. Maybe I just do not know how to cook? ..
Who faced with the need to physically kill objects, tell me how to get rid of him at the right time, and not when the garbage collector himself considers it necessary.
Thank you in advance.
UPD
That's what I see on the server
Server Version Info --------------------------------------------------------------------------- Server Version: WI-V2.5.1.26351 Firebird 2.5 Server Implementation: Firebird/x86/Windows NT Service Version: 2 Configuration Info --------------------------------------------------------------------------- Base File: D:\Program Files\Firebird_2_5\ Lock File: C:\ProgramData\firebird\ Message File: D:\Program Files\Firebird_2_5\ Security Database: D:\Program Files\Firebird_2_5\security2.fdb Database Info --------------------------------------------------------------------------- Number of connections: 140 Number of databases: 140
Here is a test program which I checked on your advice:
FbConnectionStringBuilder builder = new FbConnectionStringBuilder(); builder.Dialect = 3; //builder.DataSource = "localhost"; builder.Password = "masterkey"; builder.UserID = "SYSDBA"; builder.Charset = "WIN1251"; for (int i = 0; i < fileList.Count; i++) { builder.Database = fileList[i]; using (FbConnection connection = new FbConnection(builder.ConnectionString)) { try { Console.WriteLine(string.Format("[{0}] - {1}", i.ToString("0000"), fileList[i])); connection.Open(); Console.WriteLine(string.Format("[{0}] Connected", i.ToString("0000"))); } catch (Exception ex) { Console.WriteLine(string.Format("[{0}] - EXCEPTION {1}" ,i.ToString("0000"), ex.Message)); Console.ReadKey(); } connection.Close(); Console.WriteLine(string.Format("[{0}] Disconnected", i.ToString("0000"))); } GC.Collect(); GC.WaitForPendingFinalizers(); } Console.ReadKey();
IDisposable
interface is intended so that you can destroy an object by command. It is in this method that the connection should break, and if it has not been called explicitly, then in the finalizer. If the provider does not allow creating many objects, even nailing them, it is unlikely that deletion from memory will save. - ModusFbConnection
holds not only managed resources, but also unmanaged resources that cannot be accessed through theGC
. - Modus