哪里是我的记忆?重新初始化数据表 [英] Where is my memory? re-initializing DataTable

查看:184
本文介绍了哪里是我的记忆?重新初始化数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有1 DataGridView中,1数据表等应用程序



我的执行方法(主编):

 私人无效btnRunSQL_click()
{
串strConnStr = tbConnStr.Text; //从文本框
字符串连接字符串STRSQL = tbSql.Text; //从文本框

的SqlDataAdapter的DataAdapter =新SqlDataAdapter的(STRSQL,strConnStr)查询;
SqlCommandBuilder CommandBuilder的=新SqlCommandBuilder(DataAdapter的); ;

//清理内存
// dtData的DataTable在主窗体类
dtData =新的DataTable()声明
DataAdapter.Fill方法(dtData);

showMemoryUsage();

}

这是怎么IM检查内存:

 公共无效showMemoryUsage()
{
过程PROC = Process.GetCurrentProcess();
this.Text =峰值记忆:+ proc.PeakWorkingSet64 / 1024/1024 +MB;
Application.DoEvents(); //力的形式刷新
}

当我运行这个函数多次 - 它使用了更多的和更多的内存。进出口工作在非常大的数据集(1000000行)和几大疑问我不得不重新启动我的应用程序了。



运行1M行查询后我有900MB内存的使用,第二次运行有1,100MB,1300MB等。



我想重新initalizing DataTable中会释放我的记忆,但事实并非如此。于是我重新初始化的BindingSource(带连接的DataGridView),但它没有帮助了。最后我评论我的BindingSource和DataGridView的



后来补充:



处置的DataAdapter没有帮助



我删除的DataGridView和有约束力的来源。没有帮助。



解决方案(我合并一些答案,并没有泄漏创建的测试应用程序)



 使用系统; 
使用System.Data这;使用System.Windows.Forms的
;
使用System.Data.SqlClient的;使用System.Diagnostics程序
;

//运行此代码,你需要窗体和控件:

//文本框tbConnStr - 文本与SQL Server的连接字符串
//文本框tbSQL - 为SQL查询运行/测试
//文本框tbLog - 日志显示
//按钮btnRunSQL设置为适当的方法
//按钮btnRunTest设置为适当的方法$ b $ OnClick事件OnClick事件b
命名空间Test_datatable
{
公共部分Form1类:表格
{

的DataTable dt的; //我需要这个全球

公共Form1中()
{
的InitializeComponent();
}

私人无效btnRunSQL_Click(对象发件人,EventArgs五)
{
日志(方法启动。);

串strConnStr = tbConnStr.Text;
字符串STRSQL = tbSQL.Text;

使用(SqlDataAdapter的大=新SqlDataAdapter的(STRSQL,strConnStr))
{
使用(SqlCommandBuilder CB =新SqlCommandBuilder(DA))
{

如果(DT!= NULL)
{
dt.Clear();
dt.Dispose();
日志(数据表清零和布置。);
}

DT =新的DataTable();
da.Fill(DT);
日志(数据表填写。);

}
}

日志(方法结束。);
tbLog.Text + = Environment.NewLine;

}

//打印在文本框
私人无效日志(字符串文本)
{
tbLog时间,字符串和内存使用情况。文字+ = DateTime.Now.ToString()
++文字+内存()+
Environment.NewLine;

Application.DoEvents(); //力的形式刷新
}

//返回内存使用字符串,例如:内存:123MB
私人字符串内存()
{
工艺PROC = Process.GetCurrentProcess();
返回峰值记忆:+(proc.PeakWorkingSet64 / 1024/1024)的ToString()+MB;

}为10次
私人无效btnRunTest_Click(对象发件人,EventArgs五)
{


//测试方法(INT I = 0; I&小于10;我++)
{
btnRunSQL_Click(新的对象(),新的EventArgs());
}
}
}
}


解决方案

最佳答案:依然存在着通过绑定从GUI旧数据表的链接。但实际的代码丢失。



我在这方面首选的方法:

 如果(BS! = NULL)bs.Clear(); //最有可能的解决方案
如果(dtData!= NULL)dtData.Clear(); //不会伤害

dtData =新的DataTable();
BS =新的BindingSource();


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

解决方案

Best guess: there remain links from the GUI to the old DataTables through the Bindings. But the actual code is missing.

My preferred approach in this context:

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

dtData = new DataTable(); 
bs = new BindingSource(); 

这篇关于哪里是我的记忆?重新初始化数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆