473,699 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MatchCollection Memory Issue

In the program below, when the control comes back to the Main program,
a considerable amount of memory is still associated with the program.
Is this normal? Is it due to repeated generation of MatchCollection
objects?

This program is a sample of a larger problem I am facing with a
document retrieval application. The algorithm is as follows:
1. Recusively traverse a directory
2. Obtain the text of every file in a string
3. Extract "terms" in the string using the expression in Regex
4. Loop through MatchCollection to obtain number of occurences of each
"term"

Through a memory profiling tool, I noticed considerable profusion of
strings, which I think is due to MatchCollection . How do I ensure that
memory occupied by MatchCollection is reclaimed often?

Thanks, Kini

*** Code ***

using System;
using System.Collecti ons;
using System.Text.Reg ularExpressions ;

namespace DemoRegex
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
for(int i=0;i<10000;i++ )
{
FileConvert();
}
Console.WriteLi ne("Press Any Key To End");
Console.ReadLin e();
}
static void FileConvert()
{
string testString = "Fractions can be expressed as a
numerator over a denominator; however, storing them as a floating-point
value might be necessary. Storing fractions as floating-point values
introduces rounding errors that make it difficult to perform
comparisons. Expressing the value as a fraction (e.g., 1/6) allows the
maximum precision. Expressing the value as a floating-point value
(e.g., 0.16667) can limit the precision of the value. In this case, the
precision depends on the number of digits that the developer decides to
use to the right of the decimal point.";

Regex regEx = new
Regex(@"[a-zA-Z0-9]*[.\-_]*[.a-zA-Z\-_][.a-zA-Z0-9\-_]*[a-zA-Z0-9]");
MatchCollection matchesInDoc = regEx.Matches(t estString);
}
}
}

Nov 17 '05 #1
6 2484
Hi Kini,
usually the Garbage Collector will execute when it thinks it is time to
reclaim unused memory. Are you seeing some kind of degredation in the
program as a result of large amounts of memory being consumed, how much is
being used compared to the amount of RAM you have?

You can force the Garbage collector to explicitly run at any time in your
code by calling GC.Collect();

Mark.

"Kini" wrote:
In the program below, when the control comes back to the Main program,
a considerable amount of memory is still associated with the program.
Is this normal? Is it due to repeated generation of MatchCollection
objects?

This program is a sample of a larger problem I am facing with a
document retrieval application. The algorithm is as follows:
1. Recusively traverse a directory
2. Obtain the text of every file in a string
3. Extract "terms" in the string using the expression in Regex
4. Loop through MatchCollection to obtain number of occurences of each
"term"

Through a memory profiling tool, I noticed considerable profusion of
strings, which I think is due to MatchCollection . How do I ensure that
memory occupied by MatchCollection is reclaimed often?

Thanks, Kini

*** Code ***

using System;
using System.Collecti ons;
using System.Text.Reg ularExpressions ;

namespace DemoRegex
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
for(int i=0;i<10000;i++ )
{
FileConvert();
}
Console.WriteLi ne("Press Any Key To End");
Console.ReadLin e();
}
static void FileConvert()
{
string testString = "Fractions can be expressed as a
numerator over a denominator; however, storing them as a floating-point
value might be necessary. Storing fractions as floating-point values
introduces rounding errors that make it difficult to perform
comparisons. Expressing the value as a fraction (e.g., 1/6) allows the
maximum precision. Expressing the value as a floating-point value
(e.g., 0.16667) can limit the precision of the value. In this case, the
precision depends on the number of digits that the developer decides to
use to the right of the decimal point.";

Regex regEx = new
Regex(@"[a-zA-Z0-9]*[.\-_]*[.a-zA-Z\-_][.a-zA-Z0-9\-_]*[a-zA-Z0-9]");
MatchCollection matchesInDoc = regEx.Matches(t estString);
}
}
}

Nov 17 '05 #2
Hello Mark,

I have 500MB of RAM on my PC.

For a particular example, using 59 files of net size 43MB, my
application's memory consumption peaks to 111MB. At the place where I
provide a breakpoint, I know that the only objects in scope are not
more than 10MB in size. I do not understand why the system does not let
go of the remaining memory :-? :(

Thanks, Kini

Nov 17 '05 #3
Hi Kini,
the nature of Garbage collection is that it is non-deterministic to the
application you do not know when it will be run. The GC has an algorithm
sitting behind it that decides when it is time to clean up the managed heap.

If you have 500MB of RAM and you are using 111MB and don't having other
apps competing for the RAM resource then the GC may decide that there is no
need to run. You don't want the Garbage Collector to be running too often
because this may hurt performance more than using more RAM, because while the
Garbage Collector is running no other threads are allowed to run, this is
because the garbage collector is removing items from the managed heap and
moving objects around in memory when it finishes it then has to reset all the
pointers to those objects to the objects new memory location before you are
allowed to continue using them.

In general the Garbage Collector is probably doing an optimum job and I
would not worry about the amount of memory you used. It might be interesting
to open up a lot of applications on your computer such that a lot of RAM is
being used and see if your application still grows to 111MB or if the GC
tidies up before that due to the limited resources.

Mark.

"Kini" wrote:
Hello Mark,

I have 500MB of RAM on my PC.

For a particular example, using 59 files of net size 43MB, my
application's memory consumption peaks to 111MB. At the place where I
provide a breakpoint, I know that the only objects in scope are not
more than 10MB in size. I do not understand why the system does not let
go of the remaining memory :-? :(

Thanks, Kini

Nov 17 '05 #4
Not sure what you mean with a considerable amount of memory, when I run your
code it's working set starts with ~5MB and grows to ~6MB. The GC ran ~30
times to collect Gen0 and once to collect Gen1. That doesn't look like a
considerable amount though.
Note that you have to be more explicit when talking about memory, what kind
of memory are you talking about, there is managed heap memory and there is
native heap memory. All the different memory types have their own
performance counters and you can watch them using perfmon.

Willy.

"Kini" <ki********@gma il.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
In the program below, when the control comes back to the Main program,
a considerable amount of memory is still associated with the program.
Is this normal? Is it due to repeated generation of MatchCollection
objects?

This program is a sample of a larger problem I am facing with a
document retrieval application. The algorithm is as follows:
1. Recusively traverse a directory
2. Obtain the text of every file in a string
3. Extract "terms" in the string using the expression in Regex
4. Loop through MatchCollection to obtain number of occurences of each
"term"

Through a memory profiling tool, I noticed considerable profusion of
strings, which I think is due to MatchCollection . How do I ensure that
memory occupied by MatchCollection is reclaimed often?

Thanks, Kini

*** Code ***

using System;
using System.Collecti ons;
using System.Text.Reg ularExpressions ;

namespace DemoRegex
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
for(int i=0;i<10000;i++ )
{
FileConvert();
}
Console.WriteLi ne("Press Any Key To End");
Console.ReadLin e();
}
static void FileConvert()
{
string testString = "Fractions can be expressed as a
numerator over a denominator; however, storing them as a floating-point
value might be necessary. Storing fractions as floating-point values
introduces rounding errors that make it difficult to perform
comparisons. Expressing the value as a fraction (e.g., 1/6) allows the
maximum precision. Expressing the value as a floating-point value
(e.g., 0.16667) can limit the precision of the value. In this case, the
precision depends on the number of digits that the developer decides to
use to the right of the decimal point.";

Regex regEx = new
Regex(@"[a-zA-Z0-9]*[.\-_]*[.a-zA-Z\-_][.a-zA-Z0-9\-_]*[a-zA-Z0-9]");
MatchCollection matchesInDoc = regEx.Matches(t estString);
}
}
}

Nov 17 '05 #5
Hello Mark and Willy,

Thank you for taking time to look into this issue and offering
suggestions. After introducing Disposer methods for some of my classes
(which includes explicit calls to garbage collector), the memory
consumtpion (I guess managed heap) appears to be modest. This is with
repsect to my application's code.

Thanks, Kini

Nov 17 '05 #6

"Kini" <ki********@gma il.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Hello Mark and Willy,

Thank you for taking time to look into this issue and offering
suggestions. After introducing Disposer methods for some of my classes
(which includes explicit calls to garbage collector), the memory
consumtpion (I guess managed heap) appears to be modest. This is with
repsect to my application's code.

Thanks, Kini


Well, this is exactly what you should NOT do, there are very few reasons to
call GC.Collect() from user code. If your classes don't own unmanaged
resources, there is no reason to implement the disposable pattern either.

Willy.
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2344
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but not necessary give it back to the OS. It seems that .NET win app will only return memory to the OS when the OS is asking for it. But!!! When the OS is asking for it is usually too late, tons of swapping and slow performance.
0
524
by: Ricardo | last post by:
How cai i copy the match.results("$1") from an entire matchcollection to a string array??? I entered the command mcMatches.copyto(sArray,0) but it won´t work.
1
597
by: Mortimer Schnurd | last post by:
Has anyone had any luck getting this CopyTo method to work? I can iterate through a MatchCollection and move each Match.Value to the System.Array without a problem. I just can't figure out why the CopyTo method will not work. Example: // Find all the Tables in an html string (page) MatchCollection mc = Regex.Matches(html, "<table.+</table"); string t = new string;
0
1171
by: Victoria Kagansky | last post by:
Hi! Has anybody seen the following problem? The Regex.Matches function returns a MatchCollection object containing all successful matches of the expression in provided string. In several cases the returned MatchCollection object seems to be corrupted. Access to its Count property or attempt to iterate through it block the function, which appears to keep running and to consume a lot of CPU. Thanks, Victoria
8
8543
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of each object within my JS app to help locate my problem? Thanks
2
6667
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication) instead of memory? (Is IPC harder to maintain / handle if it is made through memory, when compared to communication through sockets?) Some background things:
3
3523
by: san | last post by:
we cannot stop the application from increasingly use memory. The CRM Worker process will continually consume memory but not release it back to the system. Please research into how to make the application consume less memory or at least release the memory when it is done processing a request. It seem like you can be logged in for any amount of time without an issue, I've been told you shouldn't be logged out until idle for 2 hours but...
7
2806
by: =?Utf-8?B?Tmlrb2xheSBFdnNlZXY=?= | last post by:
Hi! I know this topic has been discussed a long way, but I haven't found any apparent solution (maybe I shouldn't be looking for a one :)) I have a very simple application with one page and with just ScriptManager object in it. Every time I refresh the page the memory usage goes up. I have AJAX extenstions 1.0 installed and IE7 on winxp. Is this IE or AJAX problem, and how did you guys cope with this problem? Please reply asap... i am...
10
2067
by: Andy B | last post by:
Is it safe to make a method that returns a match collection or nothing? or is it better to just return a match collection and have the code outside the method validate that match collection is empty or not?
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.