473,473 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strange memory problem in threaded application.

Hi,

I almost feel like giving up on developing on dotnet. I have done
several threaded programs with the goal to parse webpages and I have
tried WebClient and then WinSock. Both programs has memory leaks not
handled by the Garbage Collector. I don't know why my program eats so
much memory. I start the program with about 22 MB and when running about
50 threads I'm up to about 100 Mb in 30 seconds then it flattens out and
when running for 30 minutes I got about 300 Mb of used memory.

Are there any windows controls that has this memory problems ?
I use textbox and listbox. The listbox contain all the links that should
be parsed. I add links to the end and parse links in the beginning.

I'm grateful for any help you can give me on this.

Yours sincerely
Andla

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
12 1653
Hi,

It depends of your code I think, if you keep adding threads it will increase
the memory comsuption, also if you keep references to resources they will
not be release.

The best way to get those problems is using a profiler, take a look at the
memory profier found in http://www.scitech.se/memprofiler
I tested it and it gave me a very good idea of how the memory was being
consumed.

You could also post the code and others will check it and see what is wrong
with it.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Andla Rand" <an******@yahoo.se> wrote in message
news:u%****************@TK2MSFTNGP11.phx.gbl...
Hi,

I almost feel like giving up on developing on dotnet. I have done
several threaded programs with the goal to parse webpages and I have
tried WebClient and then WinSock. Both programs has memory leaks not
handled by the Garbage Collector. I don't know why my program eats so
much memory. I start the program with about 22 MB and when running about
50 threads I'm up to about 100 Mb in 30 seconds then it flattens out and
when running for 30 minutes I got about 300 Mb of used memory.

Are there any windows controls that has this memory problems ?
I use textbox and listbox. The listbox contain all the links that should
be parsed. I add links to the end and parse links in the beginning.

I'm grateful for any help you can give me on this.

Yours sincerely
Andla

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Hi,

As far as I know, the garbage collector will start freeing memory only when
the framework is unable to allocate a new chunk of memory for an object
being created. That's why your application, provided you have enough RAM,
can run with 300 MB used memory and it is not due to memory leaks. You can
check, however, that you Dispose() all classes dealing with unmanaged
resources to prevent resource leaks (that can in turn cause memory leaks).

P.S. I have also heard the garbage collector works differently on server and
workstation OSes...this might also have some effect on the memory
allocation/freeing policy.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Andla Rand" <an******@yahoo.se> wrote in message
news:u%****************@TK2MSFTNGP11.phx.gbl...
Hi,

I almost feel like giving up on developing on dotnet. I have done
several threaded programs with the goal to parse webpages and I have
tried WebClient and then WinSock. Both programs has memory leaks not
handled by the Garbage Collector. I don't know why my program eats so
much memory. I start the program with about 22 MB and when running about
50 threads I'm up to about 100 Mb in 30 seconds then it flattens out and
when running for 30 minutes I got about 300 Mb of used memory.

Are there any windows controls that has this memory problems ?
I use textbox and listbox. The listbox contain all the links that should
be parsed. I add links to the end and parse links in the beginning.

I'm grateful for any help you can give me on this.

Yours sincerely
Andla

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #3
How do I release memory when GC doesnt.

Here are some sample code.
void parse(int count)//Count is the position in a list where the link
is to be parsed-
{

this.Text= count.ToString();
this.Update();

try
{

string lnk="";

System.Threading.Thread.Sleep(200);

//if(listBox1.Items.Count==0)
if(arrli.Count==0)
{
if(count>-1)
{
lnk=links[count];
}
else
{
return;
}
}
else
{
lnk=(string)arrli[0];
arrli.RemoveAt(0);
//lnk=(string)listBox1.Items[0];
//listBox1.Items.RemoveAt(0);
//Label6.Text="Count = "+listBox1.Items.Count;
Label6.Text="Count = "+arrli.Count;
Label6.Update();
}

//lnk is the url I want to recieve.

int start=lnk.IndexOf("://");
if(start==-1)
return;

start+="://".Length;
int stop=lnk.IndexOf("/",start);
if(stop==-1)
stop=lnk.Length;
string domain=lnk.Substring(start,stop-start);
string path=lnk.Substring(stop,lnk.Length-stop).TrimEnd('/');


TcpClient cli = new TcpClient();
cli.Connect(domain,80);
NetworkStream stream = cli.GetStream();
Encoding encoder = Encoding.GetEncoding( "ascii" );

Byte[] request;
if(path=="")
{
request = encoder.GetBytes("GET / HTTP/1.0\r\nHost:
"+domain+"\r\n\r\n");
}
else
{

request = encoder.GetBytes("GET "+path+" HTTP/1.0\r\nHost:
"+domain+"\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0;
Windows)\r\n\r\n");
}
stream.Flush();
stream.Write(request, 0, request.Length);
while(!stream.DataAvailable)
{
Console.WriteLine("WAIT");
Thread.Sleep(1000);
}

int buffcount = 0;
Byte[] buffer = new byte[1024];
String response = String.Empty;
encoder = Encoding.GetEncoding( "iso-8859-1" );
while((buffcount = stream.Read(buffer, 0, buffer.Length)) > 0)
response += encoder.GetString(buffer, 0, buffcount);

stream.Close();
cli.Close();


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
Andla Rand <an******@yahoo.se> wrote:
How do I release memory when GC doesnt.


You don't. However, using a StringBuilder rather than string
concatenation would help things. Also note that a simpler way of
getting the ASCII encoding is to just use the static property
Encoding.ASCII. Further, a using(...) construct would make sure that
your streams get closed even if an exception is thrown. Finally, note
that this looks like it could be a long-running task - and so it
shouldn't be done in the UI thread, whereas the manipulation of the UI
elements in the method *should* be done in the UI thread (using
Control.Invoke).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Thanks for you answer.

I suppose that the using(...) is only for objects that inherit
IDisposible and I suppose these objects has a Dispose() method. The only
Dispose method I saw was in the listbox1 control but I rather leave that
alone :-). Thanks for your answer it makes me feal better and hopefully
leads to a programfix at the end. I read that Service Pack 2 should fix
the 'out of memory' error (extreamly bad error resulting in if program
is running in debug mode it just ends without any warnings leaving only
one line in the debugger output 'out of memory error')

Best regards
Andla

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #6
Andla Rand <an******@yahoo.se> wrote:
Thanks for you answer.

I suppose that the using(...) is only for objects that inherit
IDisposible and I suppose these objects has a Dispose() method.
Yes.
The only
Dispose method I saw was in the listbox1 control but I rather leave that
alone :-)
Have a look at Stream - that implements IDisposable too.
Thanks for your answer it makes me feal better and hopefully
leads to a programfix at the end. I read that Service Pack 2 should fix
the 'out of memory' error (extreamly bad error resulting in if program
is running in debug mode it just ends without any warnings leaving only
one line in the debugger output 'out of memory error')


That's possible, yes. I wouldn't have thought your current code
*should* actually leak memory in the first place, but the suggestions I
made should help a bit anyway - certainly avoiding repeated string
concatenation will help to stop so much "memory churn" in the first
place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
I think this is a bit misleading. If you watch the GC performance counter on
an app, you can see GCs happening fairly frequently. It doesn't wait till
there's literally no more memory left in the system, which is the impression
I got from your post.

I read somewhere that it runs when the gen 0 heap is full, and hence I guess
it will expand the gen 0 heap size as required if the app is hungry. I don't
know if that's correct or not. Mostly you hear about the GC reacting to
"memory pressure", meaning that it will run more frequently when there is
more stress on the memory, but I haven't really seen a definitive
description of when the GC decides to come out of hibernation.

Niall

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

As far as I know, the garbage collector will start freeing memory only when the framework is unable to allocate a new chunk of memory for an object
being created. That's why your application, provided you have enough RAM,
can run with 300 MB used memory and it is not due to memory leaks. You can
check, however, that you Dispose() all classes dealing with unmanaged
resources to prevent resource leaks (that can in turn cause memory leaks).

P.S. I have also heard the garbage collector works differently on server and workstation OSes...this might also have some effect on the memory
allocation/freeing policy.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Andla Rand" <an******@yahoo.se> wrote in message
news:u%****************@TK2MSFTNGP11.phx.gbl...
Hi,

I almost feel like giving up on developing on dotnet. I have done
several threaded programs with the goal to parse webpages and I have
tried WebClient and then WinSock. Both programs has memory leaks not
handled by the Garbage Collector. I don't know why my program eats so
much memory. I start the program with about 22 MB and when running about
50 threads I'm up to about 100 Mb in 30 seconds then it flattens out and
when running for 30 minutes I got about 300 Mb of used memory.

Are there any windows controls that has this memory problems ?
I use textbox and listbox. The listbox contain all the links that should
be parsed. I add links to the end and parse links in the beginning.

I'm grateful for any help you can give me on this.

Yours sincerely
Andla

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #8
> I read somewhere that it runs when the gen 0 heap is full

This seems be closer to the truth. I will consult my Jeffrey Richter's book
when I have some free time - it, as far as I know, describes the GC
algorithm at length.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Niall" <as**@me.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I think this is a bit misleading. If you watch the GC performance counter on an app, you can see GCs happening fairly frequently. It doesn't wait till
there's literally no more memory left in the system, which is the impression I got from your post.

I read somewhere that it runs when the gen 0 heap is full, and hence I guess it will expand the gen 0 heap size as required if the app is hungry. I don't know if that's correct or not. Mostly you hear about the GC reacting to
"memory pressure", meaning that it will run more frequently when there is
more stress on the memory, but I haven't really seen a definitive
description of when the GC decides to come out of hibernation.

Niall

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2****************@TK2MSFTNGP10.phx.gbl...
Hi,

As far as I know, the garbage collector will start freeing memory only

when
the framework is unable to allocate a new chunk of memory for an object
being created. That's why your application, provided you have enough RAM, can run with 300 MB used memory and it is not due to memory leaks. You can check, however, that you Dispose() all classes dealing with unmanaged
resources to prevent resource leaks (that can in turn cause memory leaks).
P.S. I have also heard the garbage collector works differently on server

and
workstation OSes...this might also have some effect on the memory
allocation/freeing policy.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Andla Rand" <an******@yahoo.se> wrote in message
news:u%****************@TK2MSFTNGP11.phx.gbl...
Hi,

I almost feel like giving up on developing on dotnet. I have done
several threaded programs with the goal to parse webpages and I have
tried WebClient and then WinSock. Both programs has memory leaks not
handled by the Garbage Collector. I don't know why my program eats so
much memory. I start the program with about 22 MB and when running about 50 threads I'm up to about 100 Mb in 30 seconds then it flattens out and when running for 30 minutes I got about 300 Mb of used memory.

Are there any windows controls that has this memory problems ?
I use textbox and listbox. The listbox contain all the links that should be parsed. I add links to the end and parse links in the beginning.

I'm grateful for any help you can give me on this.

Yours sincerely
Andla

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Nov 15 '05 #9
In addition, I doubt that 50 threads really helps you any and likely will
hurt you more then help. Why 50 threads? Can you refactor to using a few
worker threads? This should also help memory and performace.

--
William Stacey, DNS MVP

"Andla Rand" <an******@yahoo.se> wrote in message
news:u#**************@TK2MSFTNGP11.phx.gbl...
Hi,

I almost feel like giving up on developing on dotnet. I have done
several threaded programs with the goal to parse webpages and I have
tried WebClient and then WinSock. Both programs has memory leaks not
handled by the Garbage Collector. I don't know why my program eats so
much memory. I start the program with about 22 MB and when running about
50 threads I'm up to about 100 Mb in 30 seconds then it flattens out and
when running for 30 minutes I got about 300 Mb of used memory.

Are there any windows controls that has this memory problems ?
I use textbox and listbox. The listbox contain all the links that should
be parsed. I add links to the end and parse links in the beginning.

I'm grateful for any help you can give me on this.

Yours sincerely
Andla

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #10
I Can't help myself. I love threads :-)

Seriously if one thread is waiting for a server to respond then why not
connect to another server. Now actually the bottleneck is my code rather
than downloading pages. But if it wasn't then I wouldn't even know that
threads are waiting for slow servers. 50 threads maybe is much but I'm
still tweaking it for best performance.

Regards
Andla
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #11
What about the thread pool? If a thread pool thread is blocked, the thread
pool can automatically bring another into operation. Of course, it's not
going to be perfectly tailored to your situation, but it might help.

Niall

"Andla Rand" <an******@yahoo.se> wrote in message
news:ex**************@TK2MSFTNGP09.phx.gbl...
I Can't help myself. I love threads :-)

Seriously if one thread is waiting for a server to respond then why not
connect to another server. Now actually the bottleneck is my code rather
than downloading pages. But if it wasn't then I wouldn't even know that
threads are waiting for slow servers. 50 threads maybe is much but I'm
still tweaking it for best performance.

Regards
Andla
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #12
True, if each thread is waiting for IO. Then you can fire up more requests
and let some IO device (in this case the network) handle work for a while.
Problem is, at some point you have to pay the tax. As these queued up
requests come back, your threads will all compete for CPU time to do work.
This competition (sync locks, context switches) all factor to make many
threads slower then having a few worker threads (in general.) With a single
CPU the ideal scenario would be one thread working and one thread waiting
for IO and when thread1 is done, it waits for IO and the other starts
working on the IO it was waiting for. So at all times a thread is in the
ready queue or working and one is waiting for IO. Naturally, reality does
not work so cleanly, but the idea still works better for the most part.
Most of the time, you can get more work done with less threads if you factor
your consumer/producers correctly.

--
William Stacey, DNS MVP

"Andla Rand" <an******@yahoo.se> wrote in message
news:ex**************@TK2MSFTNGP09.phx.gbl...
I Can't help myself. I love threads :-)

Seriously if one thread is waiting for a server to respond then why not
connect to another server. Now actually the bottleneck is my code rather
than downloading pages. But if it wasn't then I wouldn't even know that
threads are waiting for slow servers. 50 threads maybe is much but I'm
still tweaking it for best performance.

Regards
Andla
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #13

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

Similar topics

18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
0
by: Chris | last post by:
I'm moving an app to my server and got this strange error. The previous developer created a web service and has two C++ COM+ objects in the project. I'm getting: Access to the path...
2
by: Ryan Linneman via .NET 247 | last post by:
Any help appreciated... I am using a third party DLL file to access an HMI using ethernet. The function is declared in a VB module as: 'Read internal word address Public Declare Function...
22
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.