none
Garbage Collector Data Table RRS feed

  • Question

  • Hi Experts, 

    I have the following code that reads a special xml file format generated via SQL Server. The code runs absolutely fine and is scheduled to run as a service but when run its not freeing the memory once the file get processed as checked from the task manager memory.. The memory increases same as the size of file and stays intact. 

    I have used the dispose & set to null.

    Please advise how can I get the rid of the memory from the data table once finished execution. Am trying the released version as a service. Am also a newbie in C# so please guide. 

    using Microsoft.SqlServer.XEvent.Linq;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    
    
    public static void  QueryPerfReadXeFile(String InputFilePath)
            {
             
                var dt = new DataTable();
                dt.Columns.Add("Event_Name", typeof(string));
                dt.Columns.Add("object_name", typeof(string));
                dt.Columns.Add("object_id", typeof(Int32));
                dt.Columns.Add("object_type", typeof(string));
               
    
                
                using (var events = new QueryableXEventData(InputFilePath))
                {
                    foreach (var xe in events)
                    {
                        var row = dt.NewRow();
                        dt.Rows.Add(row);
                        row["Event_Name"] =  xe.Name;
    
                        xe.Fields.TryGetValue("object_id", out PublishedEventField object_id);
                        row["object_id"] = object_id == null ? 0 : object_id.Value;
    
                        xe.Fields.TryGetValue("object_name", out PublishedEventField object_name);
                        row["object_name"] = object_name == null ? 0 : object_name.Value;
    
    
    
                        xe.Fields.TryGetValue("object_type", out PublishedEventField object_type);
                        row["object_type"] = object_type?.Value;
    
                      
                    }
                    
    
                }
                
    
    
                dt.Dispose();
                dt = null;
    
           } 

    Thanks

    Priya

    Sunday, December 15, 2019 10:14 AM

Answers

  • DataTable does not have any unmanaged resources, so Dispose() doesn't do much. The Dispose() methods in a DataTable exists because of side effect of inheritance. DataTables suppress finalization in their constructors this is why
    calling Dispose() is pointless.

    I suspect the memory and/or performance issues are related to the the database operation(s) or the processing of the XML file.


    william xifaras

    • Marked as answer by Priya Bange Sunday, December 15, 2019 3:03 PM
    Sunday, December 15, 2019 11:26 AM
  • You need to understand that, under .NET, memory management works a bit differently to what you may be used to see in different environments. When an object becomes unreachable, it becomes available for Garbage Collection. But Garbage Collection does not occur immediately. Only when there is memory pressure (i.e., some memory needs to be allocated for something else and there is not enough of it) then the Garbage Collector is triggered and it frees some of the memory that is available to it.

    This is done on purpose to optimize performance, that is, don't bother freeing memory unless it is needed for something else. If you wish, you can force a pass of the Garbage Collector by calling GC.Collect(), but normally this is done only for testing the program (to see if all Finalizers run correctly). It is not recommended that you do this in production.

    By the way, your dt=null statement is useless. The variable dt goes out of scope immediately after, and going out of scope produces the same effect as assigning a null. So you don't need to set it to null.

    • Marked as answer by Priya Bange Sunday, December 15, 2019 3:03 PM
    Sunday, December 15, 2019 2:52 PM
    Moderator

All replies

  • DataTable does not have any unmanaged resources, so Dispose() doesn't do much. The Dispose() methods in a DataTable exists because of side effect of inheritance. DataTables suppress finalization in their constructors this is why
    calling Dispose() is pointless.

    I suspect the memory and/or performance issues are related to the the database operation(s) or the processing of the XML file.


    william xifaras

    • Marked as answer by Priya Bange Sunday, December 15, 2019 3:03 PM
    Sunday, December 15, 2019 11:26 AM
  • You need to understand that, under .NET, memory management works a bit differently to what you may be used to see in different environments. When an object becomes unreachable, it becomes available for Garbage Collection. But Garbage Collection does not occur immediately. Only when there is memory pressure (i.e., some memory needs to be allocated for something else and there is not enough of it) then the Garbage Collector is triggered and it frees some of the memory that is available to it.

    This is done on purpose to optimize performance, that is, don't bother freeing memory unless it is needed for something else. If you wish, you can force a pass of the Garbage Collector by calling GC.Collect(), but normally this is done only for testing the program (to see if all Finalizers run correctly). It is not recommended that you do this in production.

    By the way, your dt=null statement is useless. The variable dt goes out of scope immediately after, and going out of scope produces the same effect as assigning a null. So you don't need to set it to null.

    • Marked as answer by Priya Bange Sunday, December 15, 2019 3:03 PM
    Sunday, December 15, 2019 2:52 PM
    Moderator