Hye friends.

I am facing one issue related to socket program written in VB.NET (2008 + 3.5 framework) since long time....I am having a socket server application which
accepts the connection...It accepts 800 connection without any problem but after that it throws exception saying...."Exception of type 'System.OutOfMemoryException' was thrown. : mscorlib : 0"
After this exceptions it stops accepting any further connections even if all the connected clients gets disconnected. Same case
happens if single client continuously connects & disconnects at an interval of 3 sec. After 2 hrs. it throws same exeption.
I am using Windows 2003 server + 2 GB RAM. If anyone faced same problem pls help...

Recommended Answers

All 11 Replies

are you closing teh socket when client discoonnects?
mibby this link helps: http://msdn.microsoft.com/en-us/system.gc.addmemorypressure.aspx

the error System.OutOfMemoryException means exactly what it says. so check if you create/open any files and not disposing them (like client information, hashtables, datareaders and so on).
are you synclock the stream?
check the stack trace of the exception. sometimes it tells you where exactlly it hangs up.

are you closing teh socket when client discoonnects?
mibby this link helps: http://msdn.microsoft.com/en-us/system.gc.addmemorypressure.aspx

the error System.OutOfMemoryException means exactly what it says. so check if you create/open any files and not disposing them (like client information, hashtables, datareaders and so on).
are you synclock the stream?
check the stack trace of the exception. sometimes it tells you where exactlly it hangs up.

Thanks GeekByChoiCe for ur quick response. As soon as clients disconnects socket is closed carefully. I had checked all the objects are property disposed through .NET Memory profiler. Also had used monitor.enter & monitor.exit wherever needed. Also when single client gets connected & disconnected continuously Socket application memory in task manager shows MemoryUsage (14MB) & VM Size 37MB. It keeps increasing decreasing as client connects/disconnects.

could you paste the stack trace of your exception? right now its hard to tell whats wrong without any source code. so just fishing in the blue.

One of the biggest causes for memory leaking is event handlers not being removed. I highly suspect that somewhere you're not cleaning up resources. Keep up your investigations with the memory profiler

here is the stack trace parameters I got....

at System.Threading.Thread.StartInternal(IPrincipal principal, StackCrawlMark& stackMark)
at System.Threading.Thread.Start()
at SocketServer.SocketServer.Socket_OnAccept(Object sender, AcceptEventArgs e)


Also pls see the attachment of snapshots took during the time error occurred....

hmpf never tried to start 800 threads in one application tbh. didnt even come in my mind either lol
i honestly have no idea if the creating of threads are limited either.
wouldnt it be better to create a new class for each client instead of a new thread?
looking forward for snakes reply

looking forward for snakes reply

Who is snakes? I'm Scott Knake .. but not snake :P

Post your socket code here. You should never create more than 20 threads at the absolute maximum, but even that is a little insane unless you have a serious application. You should probably be using an asynchronous model for communications here instead of threads.

woeps sry :D
guess snake is just too much common :$

Today again when I started socket application under memory profiler with 7 clients connected to it which were connecting/disconnecting periodically. Almost after 2&1/2 Hr. socket application stopped taking new connection although this time did not raised any outofmemory exception. On client side application it thrown exception saying..."Exception - No connection could be made because the target machine actively refused it 100.100.100.32:8888". Since at any moment max. of 7 threads are created & destroyed periodically then why still socket application stops accepting new connection??? I am attaching snapshot of memory profiler

You're going to need to upload a project i'm afraid. These issues are far too sensitive to a single line of code causing a slight leak -- compounding over time to form a large leak -- and experience what you are seeing now. I'm not familiar with the memory profiler you're using and thus wouldn't rely on it to debug an issue, although it may help :)

Here is an example showing how you can run your system out of memory with ~7 threads:

private void button7_Click(object sender, EventArgs e)
    {
      List<Thread> lst = new List<Thread>();
      for (int i1 = 0; i1 <= 7; i1++)
      {
        Thread t = new Thread(new ThreadStart(DoWork), (262144 * 1600));
        t.Start();
        lst.Add(t);
        Thread.Sleep(1000);
      }
      MessageBox.Show("Done"); //you should crash before this
    }

.ZIP your project with the client+server so we can compile+run+duplicate the issue you're seeing.

Hi friends

I am facing same problem with socket application like Kamlesh_sj08. I am trying for long time. But I not get a positive result.
I am writing a program for tollgate that have taken readed card. So my purpose is : read card than to allow the passage of a toll gate.

I have 8 connections. First all connections connects successfully. My application work fine.(the passage of toll gate is ok) After few time (not certain time) I take this exepcition. "Exception of type 'System.OutOfMemoryException' was thrown. : mscorlib : 0"
All connections is failed and my application not work again..

I am using using System.Net,system.Net.sockets namespaces. So i wrote like connectToserver,Waitfordata, ondatareceived methods.

If you want to source code, I can give ...

What should I do??
Any one help? Please.
Thank you in advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.