它是不好的做法依赖于.NET自动化垃圾收集器? [英] Is it bad practice to depend on the .NET automated garbage collector?

查看:184
本文介绍了它是不好的做法依赖于.NET自动化垃圾收集器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能创造大量内存密集型对象,然后放弃对它们的引用。例如,我可能要下载并从数据库中的一些数据进行操作,我会做100个独立下载和处理的迭代。我可以宣布一个数据表变量一次,并为每个查询使用构造,abondoning老DataTable对象在内存中恢复到一个新的DataTable对象。

DataTable类具有易于内置的方式来释放其占用的内存,包括Rows.Clear()和.Dispose()。所以,我可以在变量设置为一个新的DataTable对象之前做到这一点,在每个迭代结束。我也可以忘记它,只是让CLR垃圾收集器帮我这个忙。垃圾收集器似乎是pretty的有效所以最终的结果应该是一样的两种方式。它是更好,以明确处置内存重物,当你不需要他们,(但增加了code要做到这一点),或仅仅依靠垃圾收集器为你做所有的工作(你在在GC算法的摆布,但你的code是更小的)?

根据要求,这里是code说明回收数据表变量例如:

  // queryList是产生其他地方100 SELECT查询列表。
    //他们每个人都将返回一百万行,10列。
    名单<字符串> queryList = GetQueries(@\\ someserver \帮邻queries.txt);
    数据表workingTable;

    使用(OdbcConnection CON =新OdbcConnection(连接字符串)){
        使用(OdbcDataAdapter的ADPT =新OdbcDataAdapter的(,CON)){
            的foreach(在queryList字符串SQL){
                workingTable =新的DataTable(); //一个新的表被创建。 previous一个被遗弃
                adpt.SelectCommand.CommandText = SQL;
                adpt.Fill(workingTable);
                CalcRankingInfo(workingTable);
                PushResultsToAnotherDatabase(workingTable);
                //在这里,我可以打电话workingTable.Dispose()或workingTable.Rows.Clear()
                //我也可以什么都不做,希望垃圾收集清理我
                //自动巨大的数据表。
            }
        }
    }
 

解决方案

好吧,及时疏通了一点东西(因为我原来的职位是有点浑浊)。

IDisposable接口无关,与内存管理。 的IDisposable 允许一个对象来清理任何本地资源,它可能会坚持着。如果一个对象实现的IDisposable ,你应该确保既可以使用使用块,或致电的Dispose()当你用它完成后。

作为定义内存密集型对象,然后失去了对它们的引用,这是多么的垃圾收集器的工作原理。这是一个很好的事情。让它发生,让垃圾收集器完成其工作。

...因此,要回答你的问题,不,它不是一个不好的做法依赖于.NET垃圾收集器。恰恰相反的事实。

It's possible to create lots of memory-intensive objects and then abandon references to them. For example, I might want to download and operate on some data from a database, and I will do 100 separate download and processing iterations. I could declare a DataTable variable once, and for each query reset it to a new DataTable object using a constructor, abondoning the old DataTable object in memory.

The DataTable class has easy built-in ways to release the memory it uses, including Rows.Clear() and .Dispose(). So I could do this at the end of every iteration before setting the variable to a new DataTable object. OR I could forget about it and just let the CLR garbage collector do this for me. The garbage collector seems to be pretty effective so the end result should be the same either way. Is it "better" to explicitly dispose of memory-heavy objects when you don't need them, (but add code to do this) or just depend on the garbage collector to do all the work for you (you are at the mercy of the GC algorithm, but your code is smaller)?

Upon request, here is code illustrating the recycled DataTable variable example:

    // queryList is list of 100 SELECT queries generated somewhere else.
    // Each of them returns a million rows with 10 columns.
    List<string> queryList = GetQueries(@"\\someserver\bunch-o-queries.txt");
    DataTable workingTable;

    using (OdbcConnection con = new OdbcConnection("a connection string")) {
        using (OdbcDataAdapter adpt = new OdbcDataAdapter("", con)) {
            foreach (string sql in queryList) {
                workingTable = new DataTable();  // A new table is created. Previous one is abandoned
                adpt.SelectCommand.CommandText = sql;
                adpt.Fill(workingTable);
                CalcRankingInfo(workingTable);
                PushResultsToAnotherDatabase(workingTable);
                // Here I could call workingTable.Dispose() or workingTable.Rows.Clear()
                // or I could do nothing and hope the garbage collector cleans up my
                // enormous DataTable automatically.
            }   
        }
    }

解决方案

Ok, time to clear things up a bit (since my original post was a little muddy).

IDisposable has nothing to do with Memory Management. IDisposable allows an object to clean up any native resources it might be holding on to. If an object implements IDisposable, you should be sure to either use a using block or call Dispose() when you're finished with it.

As for defining memory-intensive objects and then losing the references to them, that's how the Garbage Collector works. It's a good thing. Let it happen and let the Garbage Collector do its job.

...so, to answer your question, No. It is not a bad practice to depend on the .NET Garbage Collector. Quite the opposite in fact.

这篇关于它是不好的做法依赖于.NET自动化垃圾收集器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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