线程应用程序中的奇怪内存问题。 [英] Strange memory problem in threaded application.

查看:84
本文介绍了线程应用程序中的奇怪内存问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我几乎觉得放弃在dotnet上开发。我已经完成了

几个线程程序,目的是解析网页,我已经尝试过WebClient和WinSock。这两个程序都有内存泄漏,而不是由垃圾收集器处理的
。我不知道为什么我的节目吃了这么多内存。我用大约22 MB的程序启动程序,在运行时运行大约

50个线程我在30秒内达到大约100 Mb然后在运行时变平并且

30分钟我有大约300 Mb的已用内存。


是否有任何Windows控件存在内存问题?

我使用文本框和列表框。列表框包含应解析
的所有链接。我在开头添加链接到结尾和解析链接。


我很感激你能给我的任何帮助。


你的真诚

Andla


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

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!

推荐答案




我认为这取决于您的代码,如果您继续添加线程,它会增加

内存消耗,如果你保留对资源的引用,他们将不会发布



解决这些问题的最佳方法正在使用分析器,请查看中找到的

内存容量http://www.scitech.se/memprofiler


我对它进行了测试,它让我非常了解内存是如何实现的消耗了



您也可以发布代码,其他人会检查它,看看有什么问题

用它。


希望这个帮助,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通部


" Andla Rand" <一个****** @ yahoo.se>在留言中写道

news:u%**************** @ TK2MSFTNGP11.phx.gbl ...
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...


我几乎想放弃在dotnet上开发。我已经完成了几个线程程序,目的是解析网页,我已经尝试了WebClient,然后尝试了WinSock。这两个程序都有内存泄漏,而不是由垃圾收集器处理。我不知道为什么我的程序会吃太多的记忆。我以大约22 MB启动程序,当运行大约50个线程时我在30秒内达到大约100 Mb然后它变平了并且当运行30分钟时我得到大约300 Mb用过的内存。

是否有任何Windows控件存在内存问题?
我使用文本框和列表框。列表框包含应解析的所有链接。我在开头添加链接到结尾和解析链接。

我很感激你能给我的任何帮助。

你的真诚
Andla

***通过开发人员指南 http://www.developersdex.com发送 ***
不要只是参加USENET ......获得奖励!
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!






据我所知,只有当框架无法为对象分配新的内存块时,垃圾收集器才会开始释放内存
正在创建。这就是为什么你的应用程序,如果你有足够的RAM,

可以使用300 MB的内存运行,这不是由于内存泄漏。你可以检查
,但是,Dispose()所有处理非托管资源的类都会阻止资源泄漏(这可能会导致内存泄漏)。


PS我也听说过垃圾收集器在服务器和工作站操作系统上的工作方式不同......这可能对内存有一些影响

分配/释放策略。


-

Dmitriy Lapshin [C#/ .NET MVP]

X-Unity测试工作室
http://x-unity.miik.com.ua/teststudio.aspx

将单元测试的强大功能带到VS .NET IDE


" Andla Rand" <一个****** @ yahoo.se>在留言中写道

news:u%**************** @ TK2MSFTNGP11.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...


我几乎想放弃在dotnet上开发。我已经完成了几个线程程序,目的是解析网页,我已经尝试了WebClient,然后尝试了WinSock。这两个程序都有内存泄漏,而不是由垃圾收集器处理。我不知道为什么我的程序会吃太多的记忆。我以大约22 MB启动程序,当运行大约50个线程时我在30秒内达到大约100 Mb然后它变平了并且当运行30分钟时我得到大约300 Mb用过的内存。

是否有任何Windows控件存在内存问题?
我使用文本框和列表框。列表框包含应解析的所有链接。我在开头添加链接到结尾和解析链接。

我很感激你能给我的任何帮助。

你的真诚
Andla

***通过开发人员指南 http://www.developersdex.com发送 ***
不要只是参加USENET ......获得奖励!
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!






GC如何释放内存。


以下是一些示例代码。

void parse(int count)// Count是列表中的位置在哪里链接

要解析 -

{


this.Text = count.ToString();

this.Update();


尝试

{


string lnk =" " ;;


System.Threading.Thread.Sleep(200);


//if(listBox1.Items.Count==0 )

if(arrli.Count == 0)

{

if(count> -1)

{

lnk = links [cou nt];

}

其他

{

返回;

} < br $>
}

其他

{

lnk =(字符串)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是我想收到的网址。


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);

NetworkSt ream stream = cli.GetStream();

Encoding encoder = Encoding.GetEncoding(" ascii" );


Byte [] request;

if(path =="")

{

request = encoder.GetBytes(" GET / HTTP / 1.0\r\\\
Host:

" + domain +" \\\\\\\\\\\\\\\\\ n");

}

其他

{


request = encoder.GetBytes(" ; GET" + path +" HTTP / 1.0 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 6.0;

Windows)\\\\\\\ nn;);

}

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 =编码。 GetEncoding(" iso-8859-1");

while((buffcount = stream.Read(buffer) ,0,buffer.Length))> 0)

响应+ = encoder.GetString(缓冲区,0,buffcount);


stream.Close( );

cli.Close();


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ...奖励它!
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!


这篇关于线程应用程序中的奇怪内存问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆