A
A
askeet2014-04-16 12:56:46
.NET
askeet, 2014-04-16 12:56:46

Where does memory go in .NET C#?

Probably a stupid question from the outside ... but I encountered this for the first time, when examining the performance of a C # program in Process Explorer, memory is slowly leaking.
The patient is launched under the Visual Studio debugger, how can you determine where and what the memory goes to and why it cannot be freed?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
plasticmirror, 2014-04-16
@plasticmirror

dottrace.memory?
ps what is leaking? GC.Collect() releases "excess" or not? calls of any unmanaged code or processes - is there?

A
askeet, 2014-04-16
@askeet

The GS Handles counter constantly changes and grows 500 - 600 , 522 - 700 , 600 - 839 , 993 - 1100.
On the form, when updating the DataTableGrid, Dictionary <string,string> is used via the DataSource.

A
askeet, 2014-04-22
@askeet

After studying the problem, the following was revealed when writing data to the database through the ODBC Manager, there are large jumps in memory up to 30 megabytes, which are subsequently cleared more slowly than the planned access to the database. A total of 30 megabytes quickly turns into 400. But after the database access is stopped, all memory is gradually cleared.
The function of writing to the database has the following form, which is frequently accessed

public static int ExecuteNonQuery(string query)
        {
            int ret = 0;
          //  return ret;  если раскоментировать  употребление памяти не увеличивается
            lock (Lock)
            {
                try
                {
                    using (OdbcConnection Connection = new OdbcConnection(ConnectionString))
                    {
                        Connection.Open();
                        if (Connection.State == System.Data.ConnectionState.Open)
                        {
                            using (var command = new OdbcCommand(query, Connection))
                            {
                                ret = command.ExecuteNonQuery();
                            }
                        }
                        Connection.Close();
                        //OdbcConnection.ReleaseObjectPool();
                    }
                    OdbcConnection.ReleaseObjectPool();
                }
                catch (Exception e)
                {
                    Console.WriteLine("ExecuteNonQuery=" + e.Message);
                }
            }
            return ret;
        }

In this case, writing to the database is carried out asynchronously.
public static void AddRecordsInArhiveAsync(ushort Code, bool NewData, DateTime Now, bool Finish = false)
        {
                Thread _trdAddRecords = new Thread(      
                    delegate()      
                    {
                        lock (LockThis)                        
                            AddRecordsInArhive(Code, NewData, Now, Finish); // Внутри вызывается функция ExecuteNonQuery(string query)              
                    }
                );

                _trdAddRecords.Priority = ThreadPriority.Normal;
                _trdAddRecords.Start();
        }

Then an attempt was made to rewrite the function from an asynchronous call to AddRecordsInArhiveAsync to a synchronous one.
public static void AddRecordsInArhiveSync(ushort Code, bool NewData, DateTime Now, bool Finish = false)
        {
            lock (LockThis)
                AddRecordsInArhive(Code, NewData, Now, Finish);
        }

After that, the garbage collector began to work correctly.
The question is, what's wrong with the AddRecordsInArhiveAsync function?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question