2

I have application with 1 DataGridView, 1 DataTable etc.

My "execute" method (edited):

private void btnRunSQL_click()
{
        string strConnStr = tbConnStr.Text; // connection string from textbox
        string strSQL = tbSql.Text;         // query from textbox

        SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConnStr);
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

        // clean memory
        // dtData DataTable is declared in main form class
        dtData = new DataTable(); 
        dataAdapter.Fill(dtData);

        showMemoryUsage();

 }

This is how im checking memory:

 public void showMemoryUsage()
 {
        Process proc = Process.GetCurrentProcess();
        this.Text = "Peak memory: " + proc.PeakWorkingSet64 / 1024 / 1024 + "MB";
        Application.DoEvents(); // force form refresh
 }

When I run this function multiple times - it uses more and more memory. Im working on very big data set (1000000 rows) and after few big queries I have to restart my app.

After running 1M rows query I have memory use about 900MB, second run 1100MB, 1300MB etc.

I thought re-initalizing DataTable will free my memory, but it does not. So I re-initialized BindingSource (connected with DataGridView), but it not helped too. Finally i commented my BindingSource and DataGridView.

Added later:

Disposing DataAdapter not helped.

I removed DataGridView and binding source. Not helped.

SOLUTION (I merged few answers and created test app without leaks)

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;

// to run this code you need form and controls:

// TextBox tbConnStr - textbox with SQL Server connection string
// TextBox tbSQL - for SQL query to run/test
// TextBox tbLog - for log display
// Button btnRunSQL with OnClick event set to proper method
// Button btnRunTest with OnClick event set to proper method

namespace Test_datatable
{
    public partial class Form1 : Form
    {

        DataTable dt; // i need this global

        public Form1()
        {
            InitializeComponent();
        }

        private void btnRunSQL_Click(object sender, EventArgs e)
        {
            log("Method starts.");

            string strConnStr = tbConnStr.Text;
            string strSQL = tbSQL.Text;

            using (SqlDataAdapter da = new SqlDataAdapter(strSQL, strConnStr))
            {
                using (SqlCommandBuilder cb = new SqlCommandBuilder(da))
                {

                    if (dt != null)
                    {
                        dt.Clear();
                        dt.Dispose();
                        log("DataTable cleared and disposed.");
                    }

                    dt = new DataTable();
                    da.Fill(dt);
                    log("DataTable filled.");

                }
            }

            log("Method ends.");
            tbLog.Text += Environment.NewLine;

        }

        // prints time, string and memory usage on textbox
        private void log(string text)
        {
            tbLog.Text += DateTime.Now.ToString() 
                + " "  + text + memory() + 
                Environment.NewLine;

            Application.DoEvents(); // force form refresh
        }

        // returns memory use as string, example: "Memory: 123MB"
        private string memory()
        {
            Process proc = Process.GetCurrentProcess();
            return " Peak memory: " + (proc.PeakWorkingSet64 / 1024 / 1024).ToString() + "MB ";

        }

        // test method for 10 runs
        private void btnRunTest_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                btnRunSQL_Click(new object(), new EventArgs());
            }
        }
    }
}
4

4 回答 4

3

最佳猜测:仍然存在通过绑定从 GUI 到旧数据表的链接。但是缺少实际的代码。

在这种情况下我首选的方法:

if (bs != null)     bs.Clear();      // most likely solution
if (dtData != null) dtData.Clear();  // won't hurt

dtData = new DataTable(); 
bs = new BindingSource(); 
于 2013-04-26T14:06:58.890 回答
2

首先,您应该处理:

private void btnRunSQL_click()
{
        string strConnStr = tbConnStr.Text; // connection string from textbox
        string strSQL = tbSql.Text;         // query from textbox

        using(SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConnStr))
        {
           using(SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter))
           {
               // clean memory
               // dtData DataTable and BindingSource bs are declared in main form class
               dtData = new DataTable(); 
               bs = new BindingSource(); 

               dataAdapter.Fill(dtData);
          }
      }
 }

但我相信您的问题至少有一部分包含在您没有向我们展示的代码中。

编辑:请向我们展示您如何使用 BindingSource。

EDIT2:您使用的是 PeakWorkignSet64,您的应用程序是否以 64 位运行?否则,此属性将不准确。另外,请尝试System.GC.GetTotalMemory(true)告诉我们报告的内容。

于 2013-04-26T14:05:16.500 回答
0

SqlDataAdapter 和 SqlCommandBuilder 是一次性的,你可以试试这样的

private void btnRunSQL_click()
{
        string strConnStr = tbConnStr.Text; // connection string from textbox
        string strSQL = tbSql.Text;         // query from textbox

        using(var dataAdapter = new SqlDataAdapter(strSQL, strConnStr))
        using(var commandBuilder = new SqlCommandBuilder(dataAdapter)) {
             dtData = new DataTable(); 
             bs = new BindingSource(); 

             dataAdapter.Fill(dtData);
        }
}

我认为这不会解决您的内存问题,但由于 SqlDataAdapter 和 SqlCommandBuilder 是一次性的,这样做是个好主意。

于 2013-04-26T14:03:56.660 回答
-1

你为什么不重新填满你的旧的dtData

通过创建 的新实例dtData,旧的实例将在一段时间内被垃圾收集器清除。您也可以手动调用 GC,但如果有更好的解决方案,我不建议这样做。

编辑:为了使这一点更清楚,请尝试使用此功能:

private void btnRunSQL_click()
{
    string strConnStr = tbConnStr.Text; // connection string from textbox
    string strSQL = tbSql.Text;         // query from textbox

    using(SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConnStr))
    {
        using(SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter))
        {
            // edited after comment of  Jon Senchyna
            dtData.Clear();
            dataAdapter.Fill(dtData);
        }
     }
}
于 2013-04-26T13:55:43.897 回答