Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Serialization and Caching

I have a WCF service hosted in a console application. and I have a ChannelFactory to call WCF's operation Contracts.

Problem: whenever I call an operation that returns values, it seems that the value returned is cached somewhere by the service when it is serialized.

I am checking the service memory usage through the task manager under windows 7. When I call an operation that returns nothing, the memory is not increased, but when I call an operation that returns data, memory is increased and stays this way even after the data is returned to the client.

My guess is that this is a serialization caching issue?!?

like image 334
scatman Avatar asked Dec 10 '25 20:12

scatman


2 Answers

It sounds more like garbage collector hasn't run yet and because of that the memory wasn't released. Moreover when hosting WCF service in console application the GC runs in Workstation mode which can be less effective in such case.

like image 191
Ladislav Mrnka Avatar answered Dec 12 '25 10:12

Ladislav Mrnka


Memory will probably stay increased until the GC runs, which won't correspond to when data is returned to the client.

Have you tried adding a breakpoint or logging of some sort to your service method to make sure that the method is being called on each request? I don't think WCF does any caching on its own; at least, I've never had it do any in my applications that use it.


Edit:

Memory will remain in use until the garbage collector runs. If your process still has plenty of free space in its heap, then there really isn't a reason for the GC to run.

According to MSDN: http://msdn.microsoft.com/en-us/library/ee787088.aspx#conditions_for_a_garbage_collection

Garbage collection occurs when one of the following conditions is true:

  • The system has low physical memory.

  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.

  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

It is likely that you are allocating objects in the heap, but they are not being GC'd because the GC sees no reason to run (there is still open space in the heap generation, and no reason to spend time clearing it out).

However, if you can repeat your WCF call over and over and eventually get an Out of Memory exception, then that would indicate that you do indeed have a problem with references being held somewhere. In that case, I would use a memory profiler to determine what is being held onto, and by what.


Edit #2:

See also this thread: C# Thread not releasing memory

like image 35
CodingWithSpike Avatar answered Dec 12 '25 10:12

CodingWithSpike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!