正则表达式 - 内存性能 [英] Regex - Memory performance

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

问题描述




我有一个.Net应用程序,每天处理数千个Xml节点

天,我使用的每个节点大约30-40个正则表达式匹配,看看他们是否满足某些条件不是
。这些正则表达式匹配在循环中称为

(如if或for)。例如


for(int i = 0; i< 10; i ++)

{

Regex r = new Regex( );

r.Match(...,...);

}


我假设这些正则表达式对象应该被GC删除。进程

内存在TaskManager中不断增加,所以我决定使用.Net内存分析器检查

性能。它告诉我的是,经过几次迭代后,
内存中有90,000个奇怪的Regex对象。并且

有很多GC调用(如.Net profiler所示)。我没有使用

Compiled Regex。是否有任何理由不删除这些内容。但是,

我使用静态变量克服了这个问题,但只想在这些问题的底部找到

,以便我可以更好地理解。


谢谢

Jeevan

Hi

I have an .Net application which processes thousands of Xml nodes each
day and for each node I am using around 30-40 Regex matches to see if
they satisfy some conditions are not. These Regex matches are called
within a loop (like if or for). E.g.

for(int i = 0; i < 10; i++)
{
Regex r = new Regex();
r.Match(..., ...);
}

I assumed that these Regex objects should be deleted by GC. Process
memory keeps on increasing in TaskManager so I decided to check the
performance using .Net Memory profiler. What it showed me is that,
after few iterations, there are 90,000 odd Regex objects in memory. And
there are many GC calls (as shown by the .Net profiler). I am not using
Compiled Regex. Is there any reason why these are not deleted. However,
I overcame this problem using Static variables but just want to get to
the bottom of these so that I might have a better understanding.

Thanks
Jeevan

推荐答案

>您好

我有一个.Net应用程序,它每天处理数千个Xml节点,对于每个节点,我使用大约30-40个正则表达式匹配,看看是否
他们满足一些条件不是。这些正则表达式匹配在循环中称为
(如if或for)。例如

for(int i = 0; i< 10; i ++)
{
Regex r = new Regex();
r.Match(.. 。,...);
}
我假设GC应该删除这些Regex对象。进程
内存在TaskManager中不断增加,所以我决定使用.Net内存分析器来检查性能。它告诉我的是,经过几次迭代后,内存中有90,000个奇怪的Regex对象。并且
有许多GC调用(如.Net profiler所示)。我没有使用
Compiled Regex。是否有任何理由不删除这些内容。但是,
我用静态变量克服了这个问题,但只是想了解这些问题的底部,以便我可以更好地理解。

谢谢
Jeevan

I have an .Net application which processes thousands of Xml nodes each
day and for each node I am using around 30-40 Regex matches to see if
they satisfy some conditions are not. These Regex matches are called
within a loop (like if or for). E.g.

for(int i = 0; i < 10; i++)
{
Regex r = new Regex();
r.Match(..., ...);
}

I assumed that these Regex objects should be deleted by GC. Process
memory keeps on increasing in TaskManager so I decided to check the
performance using .Net Memory profiler. What it showed me is that,
after few iterations, there are 90,000 odd Regex objects in memory. And
there are many GC calls (as shown by the .Net profiler). I am not using
Compiled Regex. Is there any reason why these are not deleted. However,
I overcame this problem using Static variables but just want to get to
the bottom of these so that I might have a better understanding.

Thanks
Jeevan




有时候垃圾收集器并不是很及时收集,特别是如果CPU经常忙碌的话。您可以定期尝试
调用GC.Collect()来强制收集。我发现这很有帮助

在类似的情况下,垃圾收集器无法收集

最终造成过多的分页。

Pete



Sometimes the garbage collector isn''t very timely in collecting,
particularly if the CPU is constantly busy. You might try periodically
calling GC.Collect() to force the collection. I''ve found this to be helpful
in similar situations where the failure of the garbage collector to collect
ended up causing excessive paging.

Pete




< je ********** @ gmail.com>在消息中写道

news:11 ********************** @ z14g2000cwz.googlegr oups.com ...

<je**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...


我有一个.Net应用程序,每天处理数千个Xml节点,每个节点我使用大约30-40个Regex匹配,看看是否
他们满足一些条件不是。这些正则表达式匹配在循环中称为
(如if或for)。例如

for(int i = 0; i< 10; i ++)
{
Regex r = new Regex();
r.Match(.. 。,...);
}
我假设GC应该删除这些Regex对象。进程
内存在TaskManager中不断增加,所以我决定使用.Net内存分析器来检查性能。它告诉我的是,经过几次迭代后,内存中有90,000个奇怪的Regex对象。并且
有许多GC调用(如.Net profiler所示)。我没有使用
Compiled Regex。是否有任何理由不删除这些内容。但是,
我用静态变量克服了这个问题,但只是想了解这些问题的底部,以便我可以更好地理解。

谢谢
Jeevan
Hi

I have an .Net application which processes thousands of Xml nodes each
day and for each node I am using around 30-40 Regex matches to see if
they satisfy some conditions are not. These Regex matches are called
within a loop (like if or for). E.g.

for(int i = 0; i < 10; i++)
{
Regex r = new Regex();
r.Match(..., ...);
}

I assumed that these Regex objects should be deleted by GC. Process
memory keeps on increasing in TaskManager so I decided to check the
performance using .Net Memory profiler. What it showed me is that,
after few iterations, there are 90,000 odd Regex objects in memory. And
there are many GC calls (as shown by the .Net profiler). I am not using
Compiled Regex. Is there any reason why these are not deleted. However,
I overcame this problem using Static variables but just want to get to
the bottom of these so that I might have a better understanding.

Thanks
Jeevan




你的意思是尽管有很多GC调用,但是收集后还有90000个对象留下了

,你确定它们是RegEx对象实例吗? ?


你能发一个说明问题的样本吗?


威利。



Do you mean that despite the many GC calls there are 90000 objects left
after a collection, are you sure they are RegEx objects instances?

Could you post a sample that illustrates the issue?

Willy.


是的,尽管有很多GC调用。当然,我的节目没有在GC电话中制作

。我正在使用的.Net Memory Profiler正在进行那些GC

调用(我可以看到,因为内存正在下降,提升等等,因此打开了
)。我现在没有样品,因为我改变了我的

程序使用静态变量,以便在
内存中没有Regex对象,这里是它现在的样子


正则表达式470 26320


第一个数字是Live实例的数量,第二个是Live

大小(以字节为单位)。在我做出改变之前,这个数字曾经是90,000。

Yes, this is despite lot of GC calls. Ofcourse my program is not making
the GC calls. .Net Memory Profiler which I was using is making those GC
calls (and I can see that because memory is dropping, raising and so
on). I don''t have the sample as of right now because, I changed my
program to use Static variables so that Regex objects are not there in
memory and here is how it looks now

Regex 470 26320

First number is the number of Live instance and the second is the Live
size (in bytes). The number used to be 90,000 before I made the change.


这篇关于正则表达式 - 内存性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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