gpt4 book ai didi

c# - 为什么我的 .net 应用程序会占用 RAM?

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:37 27 4
gpt4 key购买 nike

我添加了一些 .dispose() 和 .close() 调用,它们似乎起到了一定的作用,但我仍然看到应用程序在所有操作完成后使用 600MB RAM。我错过了什么?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void CreateCSVFile(DataTable dt, string strFilePath)
{
#region Export Grid to CSV
// Create the CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter(strFilePath, false);
// First we will write the headers.
//DataTable dt = m_dsProducts.Tables[0];
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns[i]);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
int current_row = 0;
foreach (DataRow dr in dt.Rows)
{
current_row++;
if (current_row % 1000 == 0)
{
lbl_progress.Text = current_row.ToString() + " of " + dt.Rows.Count.ToString();
this.Refresh();
Application.DoEvents();
}
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
if (dr[i] is string)
{
sw.Write("\"" + dr[i].ToString().Replace(Environment.NewLine,", ").Replace("\"", "") + "\"");
}
else
{
sw.Write(dr[i].ToString());
};
}
if (i < iColCount - 1)
{
sw.Write("|");
}
}
sw.Write(sw.NewLine);
}
dt.Dispose();
sw.Close();
#endregion
}

private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
string strConn = ConfigurationManager.AppSettings["sql_connection"].ToString();
SqlConnection conn = new SqlConnection(strConn);
for (int i = 1; i <= int.Parse(ConfigurationManager.AppSettings["query_count"].ToString()); i++)
{
try
{
SqlDataAdapter da = new SqlDataAdapter(System.Configuration.ConfigurationManager.AppSettings["sql" + i.ToString()].ToString(), conn);
DataSet ds = new DataSet();
da.Fill(ds);
da.Dispose();
DataTable dt = ds.Tables[0];
ds.Dispose();
textBox1.Text += "\r\n[" + DateTime.Now.ToString("HH:mm:ss") + "] Starting sql" + i.ToString();
this.Refresh();
Application.DoEvents();
CreateCSVFile(dt, System.Configuration.ConfigurationManager.AppSettings["output_path"].ToString() + i.ToString() + ".csv");
textBox1.Text += "\r\n[" + DateTime.Now.ToString("HH:mm:ss") + "] Finished sql" + i.ToString();
dt.Dispose();
this.Refresh();
Application.DoEvents();
}
catch (Exception ex)
{
textBox1.Text += "\n" + ex.Message;
//break;
}
}
textBox1.Text += "\r\n[" + DateTime.Now.ToString("HH:mm:ss") + "] All done";
button1.Enabled = true;
}

}

最佳答案

首先我会说 Disposal 与垃圾收集无关 - 处理某些东西不会将其标记为 GC,也不会回收任何托管内存。它是一种机制,代码可以通过这种机制确定性地释放资源,通常是外部资源或非托管资源。

取自 MSDN :

Garbage collection occurs when one of the following conditions is true:

  • The system has low physical memory.
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of
    acceptable memory usage has been exceeded on the managed heap. This
    threshold is continuously adjusted as the process runs.
  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

这些是 GC 运行的条件。

我相信您对当前正在使用的 RAM 和仅具有大型工作集的应用程序感到困惑(进程的内存由操作系统提供,不一定使用)。它可能在其峰值时使用了 600Mb,并且因为没有其他东西要求更多空间,所以与您的进程相关的 RAM 被保留了下来。任务管理器不能很好地判断一个进程实际使用了多少 RAM,而只能判断它已经使用了多少 RAM。

您确实需要使用适当的内存分析器来查看在给定时间点有多少对象处于事件状态。

此外,任何实现了 IDisposable 的东西都可以在 using 语句中使用:

using (var connection = new SqlConnection(""))
{
connection.Open();
} // Dispose is called here, even on exception or return within using.

using 语句在后台简单地编译成 try/finally block 。

关于c# - 为什么我的 .net 应用程序会占用 RAM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10381014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com