在C#中需要有关dispose()的帮助 [英] Need help with dispose() in C#

查看:99
本文介绍了在C#中需要有关dispose()的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,请建议我们正在使用的引用,特别是在我们使用SqlCommand等的'new'关键字的地方,如果我们明确处理引用或用'using()'或当它失去范围时会收集垃圾。



In the below code please suggest the references which we are using and especially where ever we are using the 'new' keyword for the SqlCommand etc., should we explicitly dispose of the references or enclose them with 'using ()' or will it get garbage collected when it loses scope.

private static Boolean clsinit()
{
    Boolean mret = true;
    String mconnstr = @"Data Source=PL\clsSQL,7000;Initial Catalog=clsms;User
    ID=1111;password=2222;";
    clsmain.clsconn = new SqlConnection(mconnstr);
    try
    {
        clsmain.clsconn.Open();
        SqlCommand msqlcmd = new SqlCommand("select licdate from customer",
        clsmain.clsconn);
        SqlDataReader msqldat = msqlcmd.ExecuteReader();
        msqldat.Read();
        if (DateTime.Now.Date > Convert.ToDateTime(msqldat["licdate"]).Date)
        {
            MessageBox.Show("Authentication Restricted.");
            mret = false;
        }
        else
        {
            clsmain.clsconn.Close();
            if (!pychk())
            {
                MessageBox.Show("Authentication Failed");
                mret = false;
            }
        }
    }
    catch
    {
        clsmain.oclsmsgmgr.showmsg("Error connecting Server...!|.",
        mret = false;
    }
    finally
    {
        clsmain.clsconn.Close();
    }
    loadcls mfrm = new loadcls();
    mfrm.ShowDialog();
    return mret;
}





请注意:我我们已经完成了一项任务,以找出项目中的一些内存泄漏。请在垃圾收集中用dummies语言提供一些亮点。我是.NET GC操作的新手。



提前致谢。



我的尝试:



我在网上搜索了一幅清晰的图片,但那些已经去了在我脑海里。所以可能希望用我自己的例子,如果有人澄清我能理解得更好。



Please Note : I have been given a task to figure out some memory leaks in a project. Please throw some light in a 'dummies' language on garbage collection. Im quite new to .NET GC operations.

Thanks in advance.

What I have tried:

I have searched the web for a clear picture but those have gone over my head. So probably hoping with my own example if someone clarifies I could understand better.

推荐答案

这是非常脏的代码与所有静态和全局废话。

我对该片段的印象是你正在运行你的应用程序,行 mfrm.ShowDialog(); ,我是否正确?由于您谈到的所有这些一次性类型都具有相同的功能,因此它依赖于编译器的能力来发现它们超出了范围。使用不同的函数,这很容易:当函数保留时,局部变量超出范围。我不会排除他们甚至在这里收集垃圾,但我也不会假设它。

另一方面,只调用一次的函数的内存泄漏是无关紧要的。寻找重复调用的地方 - 小内存泄漏可以总计相关数量。
That's really dirty code with all that static and global crap.
My impression from that snippet is that you are running your application in it, with the line mfrm.ShowDialog();, am I correct? Since all those disposable types you talked about are in the same function, it depends on the capabilities compiler to find out that they went out of scope. With different functions, that's easy: when the function is left, local variables are out of scope. I won't exclude that they get garbage collected even here, but I won't assume it either.
On the other hand, memory leaks of a function called once only are rather irrelevant. Look for places which are called repeatedly - small memory leaks there can sum up to relevant amounts.


这篇关于在C#中需要有关dispose()的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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