473,837 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory Leak in .NET 1.1

I've detected memory leaks in our huge .NET 1.1 C# application but
couldn't localize them directly. So I've reduced the code to the
following console application:

using System;
using System.IO;

namespace MemLeak
{
class MemLeak
{
[STAThread]
static void Main(string[] args)
{
try
{
throw new NullReferenceEx ception();
}
catch
{
}

for(int i=0; i<1000; i++)
{
using(StreamWri ter newMasterfile =
new StreamWriter(@" c:\XXX.tmp"))
{
newMasterfile.C lose();
}
}

Console.ReadLin e();
}
}
}

Also you have to create an "App.config " file which is distributed by
VS2003 into the application directory with the name of the application
followed by ".config". There may be any content, the file can be empty
too - this will not influence our result.

Compile the "Release" configuration and you'll see with a tool like
"NetMemoryProfi ler" that there are almost 1000 StreamWriter reserved at
"Console.ReadLi ne". You can wait as long as you want - they'll never be
freed. If you don't have such a tool: increase the number of loops.
Then you can use the task manager to see how the memory grows (look at
the virtual memory!). With an endless loop there will be an "Out of
virtual memory" error at the end.

This problem is not only bounded to StreamWriter. We've seen this for
database objects, simple strings and other.

We have tested this with several Windows XP SP2 Systems with different
configurations. Also with the latest fixes for Windows and .NET.

The funny thing: there will be no leaks when you use the "Debug"
configuration. Also you can omit the exception or the "App.config "
file. In these cases the memory will be freed correctly. But we will
comile in "Release" configuration, use the "App.config " and Exceptions
can't be avoided.

The last option is to switch the attribute "[STAThread]" to
"[MTAThread]". The memory is ok in the small example (why?) and also in
our huge application. But I'm not sure with the COM-components we use.
So I prefer the "[STAThread]" attribute.

There are some methods which you can tell the gabage collector to clean
up: "GC.GetTotalMem ory(...)", "GC.WaitForPend ingFinalizers() " and
"GC.Collect ()". You can find combinations in the small example to get
rid of the memory. But this is very unreliable and will not work
correctly in our huge application where late binding and remoting is
used.

After some days of trial and error, I am really at my wits' end over
this problem. So is there anybody who can help me?

Many thanks in advance
Martin Gaus

May 28 '06
30 4713
I am showing the same behavior for the first example (6mb usage when done
running)

The second levelled off at around 6.3 mb after a minute or so.

Cheers,

Greg Young
MVP - C#

"Martin" <MA*****@gmx.ne t> wrote in message
news:11******** *************@y 43g2000cwc.goog legroups.com...
Good morning and thank you Greg!

you'll find the files there:
http://savefile.com/projects/831693

There are 2 files:
- MemoryLeak1: File with the original problem as described
- MemoryLeak2: A threading test with free main thread

Just start the compiled release.
Is there anyone who can reproduce my problem?

Martin

May 29 '06 #21
I think I found the source of your problem:

try this:
for(int i=0; i<1000000; i++)
{
using(StreamWri ter newMasterfile =
new StreamWriter(@" c:\XXX.tmp"))
{
newMasterfile.C lose();
}
}

Console.WriteLi ne("done"); //<<<----

Console.ReadLin e();

Does the memory usage grow after you see "done" on the console?
Didn't you just think the program was on the ReadLine line while it was
still in the loop?
It takes quite long to execute the whole loop - the memory usage is rising
all the time and then it remains the same - because GC can't run because the
only thread is stuck on the ReadLine() and it has no reason to run, because
there's no memory pressure.

"Martin" <MA*****@gmx.ne t> wrote in message
news:11******** *************@y 43g2000cwc.goog legroups.com...
Good morning and thank you Greg!

you'll find the files there:
http://savefile.com/projects/831693

There are 2 files:
- MemoryLeak1: File with the original problem as described
- MemoryLeak2: A threading test with free main thread

Just start the compiled release.
Is there anyone who can reproduce my problem?

Martin

May 29 '06 #22
Hmmm, looks like you don't know how the GC works, I tried to explain what
basically happens when you run this sample, for more details please refer to
my other reply in this thread.
Also, did you actually run the code?, and did you watch memory consumption
using perfmon?
The ReadLine is executed after the loop terminates, the GC cannot run while
in ReadLine(), but, while executing the loop, the GC kicks in several times
(per ~40 StreamWriter instances), so what you should see is a sawtooth-like
memory allocation pattern, starting at a certain level (~5Mb) and going up
to a maximum of ~6Mb (your mileage may vary).
Anyway, none of the programs posted are leaking.

Willy.

"Lebesgue" <le******@gmail .com> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
|I think I found the source of your problem:
|
| try this:
| for(int i=0; i<1000000; i++)
| {
| using(StreamWri ter newMasterfile =
| new StreamWriter(@" c:\XXX.tmp"))
| {
| newMasterfile.C lose();
| }
| }
|
| Console.WriteLi ne("done"); //<<<----
|
| Console.ReadLin e();
|
| Does the memory usage grow after you see "done" on the console?
| Didn't you just think the program was on the ReadLine line while it was
| still in the loop?
| It takes quite long to execute the whole loop - the memory usage is rising
| all the time and then it remains the same - because GC can't run because
the
| only thread is stuck on the ReadLine() and it has no reason to run,
because
| there's no memory pressure.
|
| "Martin" <MA*****@gmx.ne t> wrote in message
| news:11******** *************@y 43g2000cwc.goog legroups.com...
| > Good morning and thank you Greg!
| >
| > you'll find the files there:
| > http://savefile.com/projects/831693
| >
| > There are 2 files:
| > - MemoryLeak1: File with the original problem as described
| > - MemoryLeak2: A threading test with free main thread
| >
| > Just start the compiled release.
| > Is there anyone who can reproduce my problem?
| >
| > Martin
| >
|
|
May 29 '06 #23
I know how the GC works and wrote what I wrote for the sake of simplicity.
I actually ran the code and saw no real memory leak - that's what I wanted
to express in my post. I am sorry you misinterpreted it.

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:e6******** ******@TK2MSFTN GP03.phx.gbl...
Hmmm, looks like you don't know how the GC works, I tried to explain what
basically happens when you run this sample, for more details please refer
to
my other reply in this thread.
Also, did you actually run the code?, and did you watch memory consumption
using perfmon?
The ReadLine is executed after the loop terminates, the GC cannot run
while
in ReadLine(), but, while executing the loop, the GC kicks in several
times
(per ~40 StreamWriter instances), so what you should see is a
sawtooth-like
memory allocation pattern, starting at a certain level (~5Mb) and going up
to a maximum of ~6Mb (your mileage may vary).
Anyway, none of the programs posted are leaking.

Willy.

"Lebesgue" <le******@gmail .com> wrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
|I think I found the source of your problem:
|
| try this:
| for(int i=0; i<1000000; i++)
| {
| using(StreamWri ter newMasterfile =
| new StreamWriter(@" c:\XXX.tmp"))
| {
| newMasterfile.C lose();
| }
| }
|
| Console.WriteLi ne("done"); //<<<----
|
| Console.ReadLin e();
|
| Does the memory usage grow after you see "done" on the console?
| Didn't you just think the program was on the ReadLine line while it was
| still in the loop?
| It takes quite long to execute the whole loop - the memory usage is
rising
| all the time and then it remains the same - because GC can't run because
the
| only thread is stuck on the ReadLine() and it has no reason to run,
because
| there's no memory pressure.
|
| "Martin" <MA*****@gmx.ne t> wrote in message
| news:11******** *************@y 43g2000cwc.goog legroups.com...
| > Good morning and thank you Greg!
| >
| > you'll find the files there:
| > http://savefile.com/projects/831693
| >
| > There are 2 files:
| > - MemoryLeak1: File with the original problem as described
| > - MemoryLeak2: A threading test with free main thread
| >
| > Just start the compiled release.
| > Is there anyone who can reproduce my problem?
| >
| > Martin
| >
|
|

May 29 '06 #24

"Lebesgue" <le******@gmail .com> wrote in message
news:en******** ******@TK2MSFTN GP04.phx.gbl...
|I know how the GC works and wrote what I wrote for the sake of simplicity.
| I actually ran the code and saw no real memory leak - that's what I wanted
| to express in my post. I am sorry you misinterpreted it.
|

Hmmmm, how could I correctly interpret this:
| It takes quite long to execute the whole loop - the memory usage is
rising
| all the time and then it remains the same - because GC can't run because
the


if you know how the GC works, you must known that the memory doesn't rise
all the time while executing the loop, because the GC kicks in several times
during execution.

Willy.

May 29 '06 #25
I've recognised now that we don't have a real memory leak. Anytime and
anyhow the StreamWriters will be kicked. Possibly I have to use on of
the GC methods - and that is not very reliable but the memory is free.
I always thought I don't have care about things like this because .NET
is good enough. Maybe I'll go back to the unmanaged world. There I know
what's going on :-(

But I've nevertheless have the problem: Our program is working like the
example (and that's running out of memory). The main thread of out
program is waiting in a console.readlin e() while the work (creating the
streamwriter) is done in a thread.

Now I look for an alternative construction. I'll avoid the
console.readlin e() in the main thread as you've suggested. Please look
at my secon example:
http://savefile.com/projects/831693

Why do I have the problem there? The main thread is looping delayed and
free to call the GC. The work (creating the streamwriter) is done in a
Thread. The breaking condition (console.readli ne) is in a second
thread. All the things I learned from the postings before is that I
will not have a problem in this situation. So why is my system running
out of virtual memory (also under pressure)?

Has anyone thought about the fact that we have the problem only in the
release configuration not in the debug?

Martin

May 30 '06 #26
To all of you who never have see an "out of virtual memory exception"
see:
http://www.savefile.com/projects/831693
I've placed some screenshots there. They're from the second example.

Maybe there is hope for you:
I've tested 10 systems yesterday with different configurations:
notebooks, VMware images, pcs and so on.

Nine of them leaked but there was one that was some different. You wont
believe: it was not leaking. It was a naked (no SP) WinXP (German)
VMWare Image. I had only installed the .NET 1.1 framework. So who is
the bastard on the others?

Please can you send me your system configuration to compare?

Thanks for all
Martin

May 30 '06 #27

"Martin" <MA*****@gmx.ne t> wrote in message
news:11******** **************@ r44g2000cwb.goo glegroups.com.. .
| I've recognised now that we don't have a real memory leak.

Sure not, but what is your problem?

Anytime and
| anyhow the StreamWriters will be kicked. Possibly I have to use on of
| the GC methods -

No you don't, the GC methods are only needed in extreme rare cases, calling
them disturbs the GC allocation scheme when done without any good reason.

and that is not very reliable but the memory is free.
| I always thought I don't have care about things like this because .NET
| is good enough.

Sure it is.

Maybe I'll go back to the unmanaged world. There I know
| what's going on :-(
|

I hope you do, but I know that memory leaks are common issues in unmanaged
world :-).

| But I've nevertheless have the problem: Our program is working like the
| example (and that's running out of memory).

Not when I run it with V1.1.4322 SP1, on clean (no anti-virus crap) XP SP2 +
latest security updates, and on W2K3 SP1 latest security updates V1.1.4322
SP1 (ran it for 15 minutes).

The memory consumption stabilizes after a couple of minutes arround ~5.812
Kb "Memory usage" ~4.740Kb "VM size" .
The memory usage in debug mode is somewhat higher (~6800Kb/5356Kb)

You must have some other problem on your system(s).

Willy.

May 30 '06 #28
That's interesting.
I've a XP without SPs and the Framework 1.1.4322.573 also without SP.
It's stable! There must be something between those two.

May 30 '06 #29

"Martin" <MA*****@gmx.ne t> wrote in message
news:11******** **************@ j73g2000cwa.goo glegroups.com.. .
| That's interesting.
| I've a XP without SPs and the Framework 1.1.4322.573 also without SP.
| It's stable! There must be something between those two.
|
For me it's stable on 1.1 with SP1 and without SP1 and V2.0.50727 too.
Really I don't believe your problem has anything to do with .NET, just try
to install SP1 on the box with V1.1 and try again, you can even install V2
and test again. Watch the CLR performance counters (CLR memory/ gen0, 1 and
2 collection frequency), the GC should run every once and a while.

Another suggestion is to stopt Norton AV, I noticed in the screendumps that
you run this on the system you used to run the failing test, could be wrong
though.

Willy.
May 30 '06 #30

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

Similar topics

8
3419
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
17
4825
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to try to see where the leak is located. For all the leaky instances, I can see the following (I...
4
6095
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password"; scope.Path.Path=@"\\pc\root\cimv2";
20
8124
by: jeevankodali | last post by:
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();
23
4582
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
8561
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
7
6942
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem is on the ManagementObjectSearch, upon we Dispose the object it seems that is not releasing the...
3
5332
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
7
15721
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
22
9374
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
9837
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10870
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10563
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
10621
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
7804
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
6998
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
5668
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...
1
4474
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
4038
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.