AlexBB

I download a stream of records as I type this note. They are punctuated by some other records. In short it is a batch of about of 265 streams. Twice in the process I get the OutOfMemoryException.

It is not the first time. When it happened before I did not have a try/catch block around the code and had to abort. Now it just goes aroung it. It is a nuisance code: a timer that gives me convenience of knowing that the things proceed at a certain pace I am used to. I also have a memory usage from PerformanceMonitor on my TabPage where all other events related to download are reflected. As far as I can tell, the memory usage is low throughout and it did not change when the exceptions were thrown.

The call stack is posted below. The entry point is the timer1_Tick delegate but the rest of it I cannot analyze meaningfully. I do not know this stuff too well.

Exception of type 'System.OutOfMemoryException' was thrown.
at Microsoft.Win32.RegistryKey.InternalGetValue(String name, Object defaultValue, Boolean doNotExpand, Boolean checkSecurity)
at Microsoft.Win32.RegistryKey.GetValue(String name)
at System.Diagnostics.PerformanceMonitor.GetData(String item)
at System.Diagnostics.PerformanceCounterLib.GetPerformanceData(String item)
at System.Diagnostics.PerformanceCounterLib.GetCategorySample(String category)
at System.Diagnostics.PerformanceCounterLib.GetCategorySample(String machine, String category)
at System.Diagnostics.PerformanceCounter.NextSample()
at System.Diagnostics.PerformanceCounter.NextValue()
at tapTCPports_1.Form1.timer1_Tick(Object sender, EventArgs ee) in D:\VCsharp_Projects\tapTCPports_1\Form1.cs:line 3957

I will appreciate any comment.

Thanks.




Re: Visual C# General System.OutOfMemoryException

Peter Ritchie

It's hard to tell without some code to look at. At you wrapping your PerformanceCounter object in a using statement or otherwise calling it's Dispose method That would ensure memory would be freed as quickly as possible. Leaving the deallocation to the GC means the GC won't deallocate until it gets around to it.




Re: Visual C# General System.OutOfMemoryException

AlexBB

Peter Ritchie wrote:
It's hard to tell without some code to look at. At you wrapping your PerformanceCounter object in a using statement or otherwise calling it's Dispose method That would ensure memory would be freed as quickly as possible. Leaving the deallocation to the GC means the GC won't deallocate until it gets around to it.

Thank you Peter. I will do exactly that (the using statement).

The offensive method is the assignment this the bar.Value (3957). Did I do it right

try
{
using ( this.theCPUCounter ) // <== actually it did not work. It rendered the Counter completely dead. I had to remove it.
{
this.progrBARCPU.Value = Convert.ToInt16 ( this.theCPUCounter.NextValue ( ) ); // <== the statement that caused the exception }
}
catch ( Exception ex )
{
Console.WriteLine ( ex.Message + " \r\n" + ex.StackTrace );

}

Also I just read this in MSDN help:

Creating or deleting a performance counter requires synchronization of the underlying code by using a named mutex. If a highly privileged application locks the named mutex, attempts to create or delete a performance counter causes the application to stop responding until the lock is released. To help avoid this problem, never grant UnmanagedCode permission to untrusted code. In addition, UnmanagedCode permission potentially allows other permissions to be bypassed and should only be granted to highly trusted code.

I do not have a Mutex set up. Is it OK to use the Performance Counter under my circumstances this.theCPUCounter is an instance of the Performance counter.






Re: Visual C# General System.OutOfMemoryException

AlexBB

Peter, I also tried this. Also a failure:

try

{

using ( PerformanceCounter CPUCounter = new PerformanceCounter ( "Processor", "% Processor Time", "_Total" ) )

{

this.progrBARCPU.Value = Convert.ToInt16 ( CPUCounter.NextValue ( ) );

}

}






Re: Visual C# General System.OutOfMemoryException

Peter Ritchie

I tried running your code in a loop and didn't see the process's memory usage continually climb. It reached a peak of about 156M virtual memory and stayed there.

Are you calling that code in a loop






Re: Visual C# General System.OutOfMemoryException

AlexBB

Peter Ritchie wrote:

I tried running your code in a loop and didn't see the process's memory usage continually climb. It reached a peak of about 156M virtual memory and stayed there.

Are you calling that code in a loop

Yes, it is called in a timer_Tick method. I assume it is what you would call "a loop." Every second it gets executed. It posts values at the progress bar. I use it to monitor the state of the download--if the process has reasonable periodicity or it got stuck (in a loop, I hate to say--but that would be another loop). Also the download may be too large for the buffer to handle, etc. It is like using the task manager performance and memory usage counters but I have them arranged on my TabPage conveniently and of good visibility to me since my eyesight is not too good. Watching this progress bar to throb is like seeing the heart of the app beating.

Out of 256 downloads (each download is triggered by one SEND command on the server side and can reach about 2 Mgb, perhaps more) I got this message twice. It kept running the batch after it as nothing had happened.

I want to get down to the bottom if it because I suspect a similarity with another problem I am facing. That one is more serious. I have to abort a very crucial run with it.

Thanks.






Re: Visual C# General System.OutOfMemoryException

Peter Ritchie

Have you tried maying CPUCounter a instance field of your form and using that instead of recreating at every Tick event (of course, calling Dispose on that field in the Form's Dispose(bool) method) I can't reproduce what you're seeing with just performance counters; so it sounds like something else is causing the memory to climb.