内存泄漏与否? [英] memory leak or not?

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

问题描述

这样的函数是否因为没有明确的结果而导致内存泄漏

设置结果= null?

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

public override string GetValueAsString()

{

StringBuilder result = new StringBuilder(10);

result.Append(Month.AsString());

result.Append(''/'');

result.Append(Day.AsString() );

result.Append(''/'');

result.Append(Year.AsString());

返回结果.ToString();

}

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


或者编译器会自动获得结果吗?现在关注焦点了吗?


最好的问候,

doc

解决方案

不,当变量超出范围时,它就有资格获得垃圾

集合。


" docschnipp" < do ******** @ newsgroup.nospamwrote in message

news:D5 ********************** ************ @ microsof t.com ...


这样的函数是否因为没有明确地导致内存泄漏? br />
设置结果= null?

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

公共覆盖字符串GetValueAsString ()

{

StringBuilder结果=新的StringBuilder(10);

result.Append(Month.AsString());

result.Append(''/'');

result.Append(Day.AsString());

result.Append(''/ '');

result.Append(Year.AsString());

返回result.ToString();

}

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


或者编译器会自动获得结果吗?现在关注焦点了吗?


最好的问候,

doc



嗨Marina,


不,当变量超出范围时,它就有资格获得垃圾

集合。


感谢您的确认。从C ++转移到哪里需要照顾

有时候一切都让我变得偏执:)


doc




docschnipp写道:


嗨Marina,


" ; Marina Levit [MVP]"写道:


不,当变量超出范围时,它就有资格获得垃圾

集合。



感谢您的确认。从C ++转移到哪里需要照顾

有时候一切都让我变得偏执:)


doc



在这种情况下,警告:除非您使用使用阻止,你不能用C ++中的巧妙技巧来做解决方案,当解析器失去焦点时,它们会让对象清理它们的资源。所以一方面,GC

意味着内存泄漏的可能性要小得多,这也意味着你的
对象可能会保留很多资源(比如文件和数据库连接)<除非你手动释放它们,否则
会更长 - 从堆栈中掉落的物品

在GC运行之前不要清理它们的资源,这可能是一点点

while。


Does such a function result in a memory leak because of not explicitely
setting result = null?
------------------
public override string GetValueAsString()
{
StringBuilder result = new StringBuilder(10);
result.Append(Month.AsString());
result.Append(''/'');
result.Append(Day.AsString());
result.Append(''/'');
result.Append(Year.AsString());
return result.ToString();
}
------------------

Or does the compiler automatically gets that "result" goes off focus now?

best regards,
doc

解决方案

No, when the variable is out of scope, it becomes eligible for garbage
collection.

"docschnipp" <do********@newsgroup.nospamwrote in message
news:D5**********************************@microsof t.com...

Does such a function result in a memory leak because of not explicitely
setting result = null?
------------------
public override string GetValueAsString()
{
StringBuilder result = new StringBuilder(10);
result.Append(Month.AsString());
result.Append(''/'');
result.Append(Day.AsString());
result.Append(''/'');
result.Append(Year.AsString());
return result.ToString();
}
------------------

Or does the compiler automatically gets that "result" goes off focus now?

best regards,
doc



Hi Marina,

"Marina Levit [MVP]" wrote:

No, when the variable is out of scope, it becomes eligible for garbage
collection.

Thanks for confirmation. Moving from C++ where one has to take care of
everything makes me paranoid sometimes :)

doc



docschnipp wrote:

Hi Marina,

"Marina Levit [MVP]" wrote:

No, when the variable is out of scope, it becomes eligible for garbage
collection.


Thanks for confirmation. Moving from C++ where one has to take care of
everything makes me paranoid sometimes :)

doc

In that case, a warning: unless you use the "Using" block, you can''t do
the neat tricks in C++ where destructors made objects clean up their
resources the moment they go out of focus. So on the one hand, GC
means memory leaking is much less likely, it also means that your
objects may retain resources (like file and DB connections) a lot
longer unless you manually free them - objects falling off the stack
don''t clean up their resources until the GC runs, which may be a little
while.


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

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