内存GC - 循环 [英] Memory GC - loop

查看:82
本文介绍了内存GC - 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


你知道什么是最佳实践请说:


代码1:

TimeSpan ts;


for(i = 0; i< 1000; i ++)

{

ts = ...;

blablabla;

}





代码2:


for(i = 0; i< 1000; i ++)

{

TimeSpan ts = ...;

blablabla;

}

在我看来,它们可能会对GC产生不同的影响。

显然,我使用了timeSpan,但它可能是其他任何东西。

您认为最好的是什么?

两种情况下GC的行为方式是否相同?


谢谢。

-

Michael

----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/

解决方案



理论上,Code1应创建一个变量,该变量仍然在整个函数的范围内,并反复覆盖其内容(除非你

在循环中有ts = new TimeSpan()),代码2会在循环的每次迭代中创建一个新的

变量,每个循环超出范围每个

时间。

就个人而言,我尽量不在循环中声明变量,但我从来没有实际检查过这会影响内存使用/运行时行为。
< br $>
此致,

Kevin Wienhold


1月24日14:06,Michael Moreno< MyFirstName.MySurn ... @ free.fr>

写道:


你好,


你知道什么是最佳实践之间请说:


代码1:


TimeSpan ts;


for( I = 0; I< 1000; i ++)

{

ts = ...;

blablabla;

}





代码2:


for(i = 0; i< 1000; i ++)

{

TimeSpan ts = ...;

blablabla;

}


在我看来,它们可能会对GC产生不同的影响。

显然,我使用的是timeSpan,但它可能是其他任何东西。

您认为最好的是什么?

两种情况下GC的行为方式相同吗?


谢谢。


-

Michael

---- http://michael.moreno.free.fr/http://port.cogolin.free.fr/




有一件事是肯定的:


代码2速度较慢,因为它会在每个代码中创建对TimeSpan ts的新引用

循环。


但是对象可能处置相同的原因,因为当你设置refrence to new

对象GC收集旧对象时。


" Michae l Moreno < My ******************* @ free.frwrote in message

news:mn *********** ************ @ free.fr ...


您好,


你知道什么是最佳实践请说:


代码1:


TimeSpan ts;


for(i = 0; i< 1000; i ++)

{

ts = ...;

blablabla;

}





代码2:


for(i = 0; i< 1000; i ++)

{

TimeSpan ts = ...;

blablabla;

}


在我看来,它们可能会对GC产生不同的影响。

显然,我使用的是timeSpan,但它可能是其他任何东西。

您认为最好的是什么?

两种情况下GC的行为方式相同吗?


谢谢。


-

Michael

----
http:// michael .moreno.free.fr /
http:// port。 cogolin.free.fr/



是的,它的行为相同,原因有两个:


首先 - 除非你使用捕获的变量。 (匿名代表),

然后变量的嵌套是编译到IL时失去的极少数事情之一 - 因为所有变量都是
前导码(" .locals init")。


秒 - TimeSpan是一个结构,不由GC处理;在

的情况下,有一个*内存地址(在堆栈上)。当你做 =新的

TimeSpan(5)",你覆盖了那个记忆。当方法退出时,回收了
堆栈,变量消失在以太网中。否

物件:没有GC。


Marc


Hello,

Would you know what is best practice please between say:

CODE 1:

TimeSpan ts;

for (i=0; i<1000; i++)
{
ts = ...;
blablabla;
}

and

CODE 2:

for (i=0; i<1000; i++)
{
TimeSpan ts = ...;
blablabla;
}
It seems to me they may both impact differently on the GC.
Clearly, I used a timeSpan but it could be anything else.
What do you think is best?
does the GC behaves the same way in both cases?

Thanks.
--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/

解决方案


In theory Code1 should create one variable that remains in scope for
the entire function and override its contents over and over (unless you
have ts = new TimeSpan() inside the loop), Code 2 would create a new
variable on each iteration of the loop that goes out of scope every
time.
Personally I try not to declare variables inside of a loop, but I never
actually checked how this influences memory usage/runtime behavior.

Sincerely,
Kevin Wienhold

On 24 Jan., 14:06, Michael Moreno <MyFirstName.MySurn...@free.fr>
wrote:

Hello,

Would you know what is best practice please between say:

CODE 1:

TimeSpan ts;

for (i=0; i<1000; i++)
{
ts = ...;
blablabla;
}

and

CODE 2:

for (i=0; i<1000; i++)
{
TimeSpan ts = ...;
blablabla;
}

It seems to me they may both impact differently on the GC.
Clearly, I used a timeSpan but it could be anything else.
What do you think is best?
does the GC behaves the same way in both cases?

Thanks.

--
Michael
----http://michael.moreno.free.fr/http://port.cogolin.free.fr/



One thing is sure:

CODE 2 is slower because it creates new reference to TimeSpan ts in each
loop.

But objects probably disposed same why, because when you set refrence to new
object GC collect old object.

"Michael Moreno" <My*******************@free.frwrote in message
news:mn***********************@free.fr...

Hello,

Would you know what is best practice please between say:

CODE 1:

TimeSpan ts;

for (i=0; i<1000; i++)
{
ts = ...;
blablabla;
}

and

CODE 2:

for (i=0; i<1000; i++)
{
TimeSpan ts = ...;
blablabla;
}
It seems to me they may both impact differently on the GC.
Clearly, I used a timeSpan but it could be anything else.
What do you think is best?
does the GC behaves the same way in both cases?

Thanks.
--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/



Yes it behaves the same, for 2 very different reasons:

First - unless you use "captured variables" (anonymous delegates),
then the nesting of variables is one of the very few things that is
lost when compiling to IL - since all variables are part of the
preamble (".locals init").

Second - TimeSpan is a struct and is not handled by the GC; in either
case there is *one* memory address (on the stack). When you do " = new
TimeSpan(5)", you over-write that memory. When the method exits the
stack is reclaimed and the variable disappears into the ether. No
objects : no GC.

Marc


这篇关于内存GC - 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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