Why am I getting OutOfMemory Exceptions???  
Author Message
DLdfrd





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

I have written/deployed a C# service using VS.NET 2003.

This service is pretty simple. It communicates via TCP with a client.

The client sends a request message. When the service receives the request, an stp is executed and a response message is returned to the client.

All of the buffers that are used are static (hard-coded) buffers (with protection for buffer overflows).

The service has multiple threads. There is a thread for each TCP connection with a given client.

I have put in code to call the Garbage Collector's Collect() method when a new TCP connection is initiated.

My (deployed) service is now getting OutOfMemory exceptions. I am not using any objects that would consume memory.

So I am at a loss for this. Who is consuming the memory

None of my classes inherit the IDisposable interface. I don'r think I need the IDisposable interface because there's nothing to be freed by the Displose() method.

Can anyone please help...

TIA




.NET Development8  
 
 
ahmedilyas





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

so does this not happen on your system or on any test machines only this specific person Have they tried to run the application on a different account, and physical computer at what point in the application does this error happen

Best practice is also to place try catch {} blocks around the code that will most likely throw an exception, details about which .NET class libraries will throw the exceptions are documented in the MSDN docs as well, which will give you a good indication of where abouts the error may occur and also give the user a better friendlier warning when you catch the errors and handle it your way



 
 
RizwanSharp





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

I have written/deployed a C# service using VS.NET 2003.

This service is pretty simple. It communicates via TCP with a client.

The client sends a request message. When the service receives the request, an stp is executed and a response message is returned to the client.

All of the buffers that are used are static (hard-coded) buffers (with protection for buffer overflows).

The service has multiple threads. There is a thread for each TCP connection with a given client.

I have put in code to call the Garbage Collector's Collect() method when a new TCP connection is initiated.

My (deployed) service is now getting OutOfMemory exceptions. I am not using any objects that would consume memory.

So I am at a loss for this. Who is consuming the memory

None of my classes inherit the IDisposable interface. I don'r think I need the IDisposable interface because there's nothing to be freed by the Displose() method.

Can anyone please help...

TIA

Without seeing code, No one can comment on this problem.

Try using Clr Profiler or Memory Profiler to Identify Memory leaks.

Memory Profiler is really easy to use.

Best Regards,



 
 
Nimrand





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

All objects consume memory. However, it may be true that none of the classes you've written and used in your program consume unmanaged memory. You can still get an OutOfMemoryException, however.
 
 
V.Tortola





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Investigating Memory Issues

Great atricle ;)

Regards.


 
 
DLdfrd





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Thanks for the interest ahmedilyas!!!

Before deploying, I tested the service regoressly (excuse my spelling). My test bed simulates 10 clients. Each test client sent over 100,000 messages to my service. I never saw any problems with memory and the service size remains about 18-20 meg.

In all my methods, I have cates for all types of MSDN-documented exceptions. Once, I get an OutOfMemory exception, I log the exception and terminate the transaction in-progress (what else can I do).

My service uses the LocalSyatem account (like most services). The customer has already changed its server for another networking problem (and if my memory is correct, the old server was having these OutOfMemory exceptions).

So, what shall I try next...



 
 
nobugz





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Do you manipulate any bitmaps in your service


 
 
DLdfrd





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Thanks for the interest nobugz...

This service is doing simple TCP sends and receives to TCP clients. There are approximately 30 clients communicating with this service.

When an incomin message is received by this service (via TCP), the service opens a database connection (MS SQL 2000), executes a stored procedure, closes the database connection and returns a response message (via TCP).

All memory object that are consumed during this process are local variables in the classes. Hence, the memory should be available for deallocating by the Garbage Collect thread.

Am I right



 
 
ahmedilyas





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

if you can, try using the using clause around the SqlCommand/connection to make sure that it will dispose of the object/close it correctly at some point/level. If you can, post the code too on where you may think the error is coming from

 
 
DLdfrd





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Here's code snippet of how I connect to the MS SQL 2000 databse:

SqlConnection sqlConnect = null;
string connectString = null;

try
{
sqlConnect = new SqlConnection();

connectString = string.Format("Server={0};Database={1};User ID={2};Password={3};Persist Security Info=True;Connection Timeout=30",
m_sqlServerName,
m_sqlDatabaseName,
m_sqlUserName,
m_sqlUserPassword);
sqlConnect.ConnectionString = connectString;
sqlConnect.Open();
}

Did I miss something...

By the way, thanks for your interest ahmedilyas...



 
 
nobugz





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Using and Dispose() is not the solution here, GC.Collect() would take care of those if you didn't use them. Take a peek at your program with the CLR Profiler, it should give you an idea what objects are being leaked.


 
 
duck thing





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Dumb question -- are you closing the connection properly once you're done with it



 
 
DLdfrd





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Here's a code snippet on how I close the database connection:

private void closeSqlConnection(ref SqlConnection sqlConnect)
{
try
{
// Close the SqlConnection object...
sqlConnect.Close();
} // try
catch......

}

I know the database connection is always being close because each method that creates a db connection has a finally clause where the database connection is closed.

Is this database close snippet OK



 
 
Chris Lyon - MS





PostPosted: ASMX Web Services and XML Serialization, Why am I getting OutOfMemory Exceptions??? Top

Rico's article on tracking down managed memory leaks may help you determine the cause of your OOMs:

http://blogs.msdn.com/ricom/archive/2004/12/10/279612.aspx

Hope that helps

-Chris