Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable memory leak

following piece of code is leaking memory even if no data is returned by database. could anyone shed some light on this. .net profiler application shows that the culprit is datatable

using (OleDbDataAdapter da = new OleDbDataAdapter("select * from external_message where status='P' and pendingthread=" + Thread.CurrentThread.ManagedThreadId, conn))
                {
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        NotificationService.Logger.Write(Logger.RdvLogLevel.Debug, (uint)Thread.CurrentThread.ManagedThreadId, "GetInputs", "Received Message Id {0} Type {1}", dr["MessageId"].ToString(), dr.Field<string>("TargetType"));
                        return new DatabaseItem { connection = conn, dataRow = dr };
                    }
                    else
                    {
                        dt.Dispose();
                    }
                }
like image 924
Mubashir Khan Avatar asked May 01 '26 03:05

Mubashir Khan


1 Answers

Probably below line cause memory leak. You have to Dispose Database Connections which holds some unmanaged data.

return new DatabaseItem { connection = conn, dataRow = dr };

If it leaking memory even if no data is return make sure you Dispose conn ? Always you have to dispose database connections.

like image 130
CharithJ Avatar answered May 03 '26 17:05

CharithJ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!