I'm tired, I don't know if is my problem or if is a framework problem

A

Anibal Acosta

I was posted this in other newsgroup but i never see in the list.
-----------------------------------------------------------------

My problem is...


I have an application (Console Application). Every second it use more and
more memory, with the .NET memory profiler I see that thousands of (Byte,
Regex, String etc) instances I have in memory.

But, when I run the GC.Collect and the the GC.WaitForPendingFinalizers
manualy, the memory is released and the number of instances of the classes
mentioned are redused to 10, 5, 15 etc

My Question is, If I have a memory leak, no matter if I run or nor the
GC.Collect and the GC.WaitForPendingFinalizer the memory should not be
release, because in teory is referenced for any other instance.

Assuming that I haven't a memory leak, what is my problem?
Why the GC wait for a manual run of GC.WaitForPendingFinalizers to release
the memory?


I have a windows 2003 multiprocessor with 512 of RAM.
If I not run manually the GC.WaitForPendingFinalizer the use of memory of my
app go to 400 MB and the virtual memory go to 700 MB, after it, a "low disk
space" messsage appear next to the clock in the task bar. I think that is
the same message "Low virtual memory" of the windows 2000.

Thanks a lot.

AA
 
N

ncaHammer

Anibal Acosta said:
Assuming that I haven't a memory leak, what is my problem?
Why the GC wait for a manual run of GC.WaitForPendingFinalizers to release
the memory?

use this class instead of the Regex class. I have only implement IsMatch.
use the same pattern ("using ..") for any other

using System;
using System.Reflection;
using System.Text.RegularExpressions;

public class RegexDisposable : Regex, IDisposable {
private static MethodInfo unCache;

public RegexDisposable(string pattern) : base(pattern) {}

public static new bool IsMatch(string input, string pattern) {
using(RegexDisposable r = new RegexDisposable(pattern)){
return r.IsMatch(input);
}
}

#region IDisposable Members

public void Dispose() {
if ( unCache == null ) {
lock(typeof(RegexDisposable)) {
if ( unCache == null ) {
unCache = typeof(Regex).GetMethod("UncacheCode",
BindingFlags.NonPublic | BindingFlags.Instance );
}
}
}
if ( unCache != null ) {
unCache.Invoke(this,null);
GC.SuppressFinalize(this);
}
}


#endregion
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top