Click here to Skip to main content
14,875,030 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:

My winService (written in C#) is consuming 1.5GB (starts from few MBs and then grows) of memory due to usage of Byte arrays.

I have tried:


but didn't work for me.

please note: I am using some third party Dlls (unmanaged code).

Please help??
Updated 8-Jun-12 2:03am
Oleksandr Kulchytskyi 8-Jun-12 7:14am
Do you call from your service any unmanaged code/resources??
Vipin_Arora 8-Jun-12 8:01am
yes, we are using third party Dlls (unmanaged code)

As a last resort (not recommended) try :
codeBegin 8-Jun-12 6:52am
to the point +5
Mehdi Gholam 8-Jun-12 7:00am
Joshi, Rushikesh 8-Jun-12 9:27am
+5 for giving a direction :)
First, what's telling you that you're service is useing 1.5GB of RAM?? If you're lookin in Task Manager, it's lying to you.

In simple terms, the .NET CLR RESERVES memory for your application, called the Managed Heap. Any objects you allocate in your app, like an array are allocated out of the Managed Heap. When you free that object, the memory is returned to the Heap, NOT to Windows! Since your application used a ton of memory in the past, the .NET CLR will try to hang on to that memory so that future allocations of any objects happen quickly.

But, if Windows needs the memory back, the .NET CLR is more than happy to return any unused Manage Heap memory back to Windows.

Task Manager looks at memory usage from Windows point of view, not from the .NET CLR.

So, if you want to see how much memory you app is REALLY using, you have to use PerfMon and the .NET Memory counters or a memory profiling tool, such as these[^], or if you're using Visual Studio 2010, you can use the built in profilers.
Along with solution 1 mentioned by Mehdi Gholam below is explanation and addition.

Try to set bytearray = null as soon as you are completing consuming it.

You have not mentioned when are you consuming bytearray and when are you setting it to Null.

In addition you also might need to Collect generation 2 objects from GC. Refer Large Object Heap Uncovered[^].

Also let us know how are you calling/passing bytearray to unmanaged code, are you passing any buffer/length. Tried to pass buffer/length.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900