字符串破坏的表现 [英] Performance on string destruction

查看:55
本文介绍了字符串破坏的表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑性能和系统资源;


我在一个包含大量XML响应的主要对象中有一个字符串字段。

当我完成它时,我想要清除这个领域给了一些缓解

到记忆。


我应该用这个:

SabreContractResponse.Remove(0,SabreContractRespon se .Length);

SabreContractResponse = null;


或只是这个:

SabreContractResponse = null;


或者您有其他建议吗?


谢谢

-


SevDer
http://www.sevder.com

.NET开发人员的新.NET源

Thinking for performance & system resources;

I have a string field inside a major object that contains big XML response.
When I am done with it, I want to clear out this field to give some relief
to memory.

Should I use this:
SabreContractResponse.Remove(0,SabreContractRespon se.Length);
SabreContractResponse = null;

or just this:
SabreContractResponse = null;

or do you have any other suggestion?

Thanks
--

SevDer
http://www.sevder.com
A new .NET Source For .NET Developers

推荐答案

将其设置为null将不会实现任何效果,除非可能有很多

在退出之前使用相同方法进行其他处理。否则,只是

让例程退出时StringBuilder引用超出范围。


删除缓冲区中的内容只有在你有意义时才有意义'重新开始

重复使用它。将StringBuilder实例设置为null将导致GC

回收整个对象,包括缓冲区*,此时

GC认为这是必要的* 。


我强调了最后一点,因为很多人陷入困境,焦急地看着记忆(经常看错了记忆指示器,

以及当它没有(似乎)被释放时有一头牛。

实际上,记忆被GC标记为可回收的时候它是不再需要



--BOB


SevDer写道:
Setting it to null will accomplish nothing unless perhaps there is a lot
of other processing in the same method before it exits. Otherwise, just
let the StringBuilder reference go out of scope when the routine exits.

Deleting what''s in the buffer would only make sense if you''re going to
reuse it. Setting the StringBuilder instance to null will cause the GC
to reclaim the whole object, including the buffer, *at such time as the
GC feels it''s necessary*.

I emphasized the last point because so many people get bogged down in
anxiously watching memory (often looking at the wrong memory indicator,
as well) and then have a cow when it doesn''t (appear to) be freed up.
In effect, memory is marked by the GC as reclaimable when it''s no longer
needed.

--Bob

SevDer wrote:
考虑性能和系统资源;

我在一个包含大量XML响应的主要对象中有一个字符串字段。
当我完成它时,我想清除这个字段以给予一些缓解。我要用这个:
SabreContractResponse.Remove(0,SabreContractRespon se.Length);
SabreContractResponse = null;

或者只是这个:
SabreContractResponse = null;

或者您还有其他建议吗?

谢谢
Thinking for performance & system resources;

I have a string field inside a major object that contains big XML response.
When I am done with it, I want to clear out this field to give some relief
to memory.

Should I use this:
SabreContractResponse.Remove(0,SabreContractRespon se.Length);
SabreContractResponse = null;

or just this:
SabreContractResponse = null;

or do you have any other suggestion?

Thanks



SevDer< se **** @ newsgroup.nospam>写道:
SevDer <se****@newsgroup.nospam> wrote:
思考性能&系统资源;

我在一个包含大量XML响应的主要对象中有一个字符串字段。
当我完成它时,我想清除这个字段以给予一些缓解。我要用这个:
SabreContractResponse.Remove(0,SabreContractRespon se.Length);
SabreContractResponse = null;

或者只是这个:
SabreContractResponse = null;


那么前选项中的第一行是不会做你想要的

到 - 它只是要返回一个空的然后你忽略的字符串。

或者你有其他任何建议吗?
Thinking for performance & system resources;

I have a string field inside a major object that contains big XML response.
When I am done with it, I want to clear out this field to give some relief
to memory.

Should I use this:
SabreContractResponse.Remove(0,SabreContractRespon se.Length);
SabreContractResponse = null;

or just this:
SabreContractResponse = null;
Well the first line in former option isn''t going to do what you want it
to - it''s just going to return an empty string which you then ignore.
or do you have any other suggestion?




这个字段是一个对象中的成员变量吗?生活为

显着更长?如果是这样,将它设置为null就是前进的方向。


然而,我很少发现成员变量

有效有一个比包含

对象更短的生命周期。你真的需要它成为一个成员变量吗?这是什么?

上下文?


-

Jon Skeet - < sk *** @ pobox。 com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Is this field a member variable in an object which is meant to live for
significantly longer? If so, setting it to null is the way forward.

However, I rarely find that it''s a good idea to have member variables
which effectively have a lifetime which is shorter than the containing
object. Do you really need it to be a member variable? What''s the
context here?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon Jon,

成员变量的生命周期比对象短。

对象与ASP.NET会话一起生成,它是应用程序中最有趣的对象。\\ b
对象。 />

关于该成员变量的一个重要的事情是,它是由线程更新的




你还建议把它引用到null吗?


-


SevDer
http://www.sevder.com

.NET的新.NET源代码开发人员

" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
Hi Jon,

The member variable lives relatively shorter than the object.
Objects lives along with the ASP.NET session, and it is the most fundemental
object to the application.

And one important thing about that member variable is that, it is being
updated by thread(s).

Do you still suggest to reference it to "null"?

--

SevDer
http://www.sevder.com
A new .NET Source For .NET Developers
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
SevDer< se **** @ newsgroup.nospam>写道:
SevDer <se****@newsgroup.nospam> wrote:
思考性能&系统资源;

我在一个包含大量XML响应的主要对象中有一个字符串字段。
当我完成它时,我想清除这个字段给出一些
缓解记忆。

我应该使用它:
SabreContractResponse.Remove(0,SabreContractRespon se.Length);
SabreContractResponse = null;

或者只是这个:
SabreContractResponse = null;
Thinking for performance & system resources;

I have a string field inside a major object that contains big XML
response.
When I am done with it, I want to clear out this field to give some
relief
to memory.

Should I use this:
SabreContractResponse.Remove(0,SabreContractRespon se.Length);
SabreContractResponse = null;

or just this:
SabreContractResponse = null;



那么前一个选项的第一行是不会做你想要的
- 它只是返回一个空字符串,然后你忽略它。



Well the first line in former option isn''t going to do what you want it
to - it''s just going to return an empty string which you then ignore.

或者你还有其他建议吗?
or do you have any other suggestion?



这个字段是一个对象中的成员变量,它意味着生存的时间要长得多吗?如果是这样,将其设置为null就是前进的方向。

但是,我很少发现成员变量
有效的生命周期短于包含
对象。你真的需要它成为一个成员变量吗?这里的背景是什么?

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要给我发邮件



Is this field a member variable in an object which is meant to live for
significantly longer? If so, setting it to null is the way forward.

However, I rarely find that it''s a good idea to have member variables
which effectively have a lifetime which is shorter than the containing
object. Do you really need it to be a member variable? What''s the
context here?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于字符串破坏的表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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