数据表内存泄漏

发布于 2024-12-08 16:19:21 字数 956 浏览 1 评论 0原文

即使数据库没有返回数据,以下代码也会泄漏内存。谁能解释一下这一点。 .net profiler 应用程序显示罪魁祸首是数据表

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();
                    }
                }

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();
                    }
                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合约呢 2024-12-15 16:19:21

可能低于线导致内存泄漏。您必须处置保存一些非托管数据的数据库连接。

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

如果即使没有返回数据也会泄漏内存,请确保您处置 conn ?您始终必须处理数据库连接。

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.

ペ泪落弦音 2024-12-15 16:19:21

我不认为这是泄漏。这些对象将消耗资源,直到它们被垃圾收集。您的 dt.Dispose() 将为垃圾收集器提供一个提示,表明您的对象不再需要,但 .Net 不会清理它,直到它感觉需要为止。

您可以通过调用 GC.Collect() 来提示垃圾收集器运行,但通常您应该让 .Net 自行清理。

I don't think that it is a leak. Such objects will consume resources until they are garbage collected. Your dt.Dispose() will provide a hint to the garbage collector that your object is no longer needed, but .Net will not clean it up until it feels like it.

You can prompt the garbage collector to run by calling GC.Collect(), but normally you should let .Net clean up after itself.

活雷疯 2024-12-15 16:19:21

Main 函数上的 [STAThread] 导致了问题。删除它,现在一切正常。

[STAThread] on the Main function was causing problem. Removed it and everything is working fine now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文