此代码强制垃圾收集器收集 [英] Does this code Force Garbage Collector to Collect

查看:72
本文介绍了此代码强制垃圾收集器收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



考虑以下代码。它会让GC真正收集吗?一个应用程序

在循环中从第三方程序集创建一个类的新实例(它具有

to)。那个班没有。目的或任何类似的方法。我想让

确保GC跟上循环。我的推理如果Thread.Sleep(1000)是

被调用; GC会优先处理它的工作,对吗?


GC.Collect();

GC.WaitForPendingFinalizers();

System.Threading.Thread.Sleep(1000);

Hi,
Considering code below. Will it make GC to actually collect. One application
creates new instances of a class from 3rd party assembly in a loop (it has
to). That class doesn''t have .Dispose or any similar method. I want to make
sure GC keeps up with the loop. My reasoning if Thread.Sleep(1000) is
called; GC will take priority it do its work, right?

GC.Collect();
GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(1000);

推荐答案

LP,

确实如此,但是你可能会损害GC'做好工作'的能力,

而不是帮助它。


详见规则# 1 at:
LP,
It does, however you may be hurting the GC''s ability to "do a good job",
rather then helping it.

For details see Rule #1 at:
http://blogs.msdn.com/ricom/archive/...29/271829.aspx
http://blogs.msdn.com/ricom/archive / ... /02/40780.aspx



请记住收集器是自我调整的,所以不要乱用它!

希望这有帮助

Jay


" LP" ; < lp@a.com>写在消息

新闻:Oq ************** @ TK2MSFTNGP09.phx.gbl ...
考虑下面的代码。它会让GC真正收集吗?一个应用程序
在循环中从第三方程序集创建一个类的新实例(它具有
)。那个班没有。目的或任何类似的方法。我想
使
确保GC跟上循环。我的推理是否调用了Thread.Sleep(1000); GC会优先考虑它的工作,对吗?

GC.Collect();
GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(1000);


Just remember that "the collector is self-tuning so don''t mess with it"!

Hope this helps
Jay

"LP" <lp@a.com> wrote in message
news:Oq**************@TK2MSFTNGP09.phx.gbl... Hi,
Considering code below. Will it make GC to actually collect. One
application
creates new instances of a class from 3rd party assembly in a loop (it has
to). That class doesn''t have .Dispose or any similar method. I want to
make
sure GC keeps up with the loop. My reasoning if Thread.Sleep(1000) is
called; GC will take priority it do its work, right?

GC.Collect();
GC.WaitForPendingFinalizers();
System.Threading.Thread.Sleep(1000);



LP,


GC将跟上对象分配,并在收集时收集它们它需要b
$ b。就像周杰伦所说,GC是基于你的分配

模式自我调整的,所以它会找到自己收集的最佳时间。


为什么呢你觉得你需要强制收集垃圾吗?


-Chris


--------------- -----


|

| LP,

|确实如此,你可能会损害GC做好工作的能力,

|而不是帮助它。

|

|有关详细信息,请参阅规则#1:

|

| >> http://blogs.msdn.com/ ricom / archive / ... 29 / 271829.aspx

| >> http://blogs.msdn.com/ ricom / archive /.../ 02 / 40780.aspx

|

|请记住收集器是自我调整所以不要乱用它!

|

|希望这有助于

|周杰伦

|

| " LP" < lp@a.com>在消息中写道

|新闻:Oq ************** @ TK2MSFTNGP09.phx.gbl ...

| >

| >考虑下面的代码。它会让GC真正收集吗?一个

| >申请

| >从循环中的第三方程序集创建一个类的新实例(



|> to)。那个班没有。目的或任何类似的方法。我想

| >制作

| >确保GC跟上循环。我的理由是Thread.Sleep(1000)是

| >所谓的; GC会优先考虑它的工作,对吗?

| >

| > GC.Collect();

| > GC.WaitForPendingFinalizers();

| > System.Threading.Thread.Sleep(1000);

| >

| >

| >

|

|

|

LP,

The GC will keep up with object allocations, and collect them when it
requires. Like Jay stated, the GC is self-tuning based on your allocation
pattern, so it will find the best time to collect by itself.

Why do you feel you need to force garbage collections?

-Chris

--------------------

|
| LP,
| It does, however you may be hurting the GC''s ability to "do a good job",
| rather then helping it.
|
| For details see Rule #1 at:
|
| >> http://blogs.msdn.com/ricom/archive/...29/271829.aspx
| >> http://blogs.msdn.com/ricom/archive/.../02/40780.aspx
|
| Just remember that "the collector is self-tuning so don''t mess with it"!
|
| Hope this helps
| Jay
|
| "LP" <lp@a.com> wrote in message
| news:Oq**************@TK2MSFTNGP09.phx.gbl...
| > Hi,
| > Considering code below. Will it make GC to actually collect. One
| > application
| > creates new instances of a class from 3rd party assembly in a loop (it
has
| > to). That class doesn''t have .Dispose or any similar method. I want to
| > make
| > sure GC keeps up with the loop. My reasoning if Thread.Sleep(1000) is
| > called; GC will take priority it do its work, right?
| >
| > GC.Collect();
| > GC.WaitForPendingFinalizers();
| > System.Threading.Thread.Sleep(1000);
| >
| >
| >
|
|
|


>为什么你觉得你需要强制垃圾收集?

如前所述,有一个循环创建第三方的新实例

对象每个实例都进行一些处理并输出数据到PDF格式的报告文件

。我们尝试在循环中使用一个实例,但它只是搞砸了报告的格式,很明显它需要一个

单独的实例每个报告文件。

到目前为止,这个程序创建了100多个PDF,最终可能会有更多,也许是数千个。这个应用程序的性能

不是一个关键问题,因为它是一个批量处理风格的应用程序,运行在

预定的基础上。

所以,我们只是试图避免潜在的记忆问题(内存不足

除外);我们已经看到了相当一部分。

"" Chris Lyon [MSFT]"" < CL *** @ online.microsoft.com>在消息中写道

新闻:SY ************** @ TK2MSFTNGXA03.phx.gbl ...
> Why do you feel you need to force garbage collections?
As stated before there is a loop creating new instances of a 3rd party
object each instances does some processing and outputs data to a report file
in PDF format. We tried having to work with one instance in the loop, but it
just screws up the formatting of the reports, it''s pretty obvious it needs a
separate instance per report file.
As of now this program creates over 100 PDFs, it''s possible that eventually
there will be much more, perhaps thousands. Performance of this application
is not a critical issue, since it''s a batch process style app which runs on
scheduled basis.
So, We''re Just trying to avoid potential memory problems (out of Memory
exceptions); we have seen a fair share of those.
""Chris Lyon [MSFT]"" <cl***@online.microsoft.com> wrote in message
news:SY**************@TK2MSFTNGXA03.phx.gbl...
LP,
GC将跟上对象分配,并在需要时收集它们。就像Jay说的那样,GC是基于你的分配模式进行自我调整的,所以它会找到自己收集的最佳时间。

为什么你觉得你需要强制垃圾收集?

-

--------------------

|
| LP,
|确实如此,你可能会损害GC做好工作的能力,
|而不是帮助它。
|
|有关详细信息,请参阅规则#1:
|
| >> http://blogs.msdn.com/ ricom / archive / ... 29 / 271829.aspx
| >> http://blogs.msdn.com/ ricom / archive /.../ 02 / 40780.aspx
|
|请记住收集器是自我调整的,所以不要乱用它!
|
|希望这有助于
|周杰伦
|
| " LP" < lp@a.com>在消息中写道
|新闻:Oq ************** @ TK2MSFTNGP09.phx.gbl ...
| >
| >考虑下面的代码。它会让GC真正收集吗?一个
| >申请
| >在循环中从第三方程序集创建类的新实例(它具有
|> to)。那个班没有。目的或任何类似的方法。我想
| >制作
| >确保GC跟上循环。我的理由是Thread.Sleep(1000)是
| >所谓的; GC会优先考虑它的工作,对吗?
| >
| > GC.Collect();
| > GC.WaitForPendingFinalizers();
| > System.Threading.Thread.Sleep(1000);
| >
| >
| >
|
|
|
LP,

The GC will keep up with object allocations, and collect them when it
requires. Like Jay stated, the GC is self-tuning based on your allocation
pattern, so it will find the best time to collect by itself.

Why do you feel you need to force garbage collections?

-Chris

--------------------

|
| LP,
| It does, however you may be hurting the GC''s ability to "do a good job",
| rather then helping it.
|
| For details see Rule #1 at:
|
| >> http://blogs.msdn.com/ricom/archive/...29/271829.aspx
| >> http://blogs.msdn.com/ricom/archive/.../02/40780.aspx
|
| Just remember that "the collector is self-tuning so don''t mess with it"!
|
| Hope this helps
| Jay
|
| "LP" <lp@a.com> wrote in message
| news:Oq**************@TK2MSFTNGP09.phx.gbl...
| > Hi,
| > Considering code below. Will it make GC to actually collect. One
| > application
| > creates new instances of a class from 3rd party assembly in a loop (it
has
| > to). That class doesn''t have .Dispose or any similar method. I want to
| > make
| > sure GC keeps up with the loop. My reasoning if Thread.Sleep(1000) is
| > called; GC will take priority it do its work, right?
| >
| > GC.Collect();
| > GC.WaitForPendingFinalizers();
| > System.Threading.Thread.Sleep(1000);
| >
| >
| >
|
|
|



这篇关于此代码强制垃圾收集器收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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