Link to home
Start Free TrialLog in
Avatar of Bytech India
Bytech India

asked on

Releasing of memory using c# service which is calling c++ dll which is full of memory leaks

Hi All

I have a query regarding  memory related issue I am getting in one of my web service.
I have made a service using c# which is calling C++ third party dll. While running the service I am getting below error.

"Memory gates checking failed because the free memory is less than 1%."

After going through in deep, i found that there is some memory leak issue exist in the dll.
do we have any way by which-

1. we can release the used memory by c++ dll in c#?
2. Within service or using IIS, can we put a check by which how much free memory, service is using we will get to know and then and there can do the forceful recycle?

any idea will help.

Thanks
Avatar of Misha
Misha
Flag of Russian Federation image

If you use web service, try to add serviceHostingEnvironment in your web.config .
https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/wcf/servicehostingenvironment
And set attribute minFreeMemoryPercentageToActivateService=0
<configuration>
  <system.serviceModel>
    <serviceHostingEnvironment ... minFreeMemoryPercentageToActivateService="0" />
  </system.serviceModel>
</configuration>

Open in new window

Avatar of Bytech India
Bytech India

ASKER

thanks Misha.. Trying same.
A memory leak by definition is memory that was allocated, but never freed back to the system. You no longer have a reference/handle to that memory anymore. The only way to reclaim that memory is by terminating the process.
The only way to reclaim that memory is by terminating the process
So long as the running process retains a handle/reference to the memory.  In many programming languages, it is extremely easy to move a reference from one place to another without reclaiming (a.k.a reallocating or destroying) the previously referenced memory space.  In these cases, the only way to reclaim the memory is by restarting the computer, going so far as to requiring a cold restart.

-saige-
How do you call your third-party dll?

-saige-
As a fast workaround, you can monitor memory usage of the service by another process (that is often called WatchDog).

But of course better to understand the source of the problem. As you've mentioned that unmanaged code is called, then you probably suspect that this native code is the reason of the leak. In such case I would recommend to profile the application using a profiler for unmanaged code. The range of such software is large: from WinDbg to Deleaker.
to add to above comments:

actually the c++ dll functions should not request memory theirselves. instead you should pass the (byte) buffer needed for the operation from c# to c++ by argument (pointer). doing so you can free the memory after use in c#. if the c# doesn't know the size of memory required. you should change the c++ functionality such that it returns the required size if call with a NULL buffer.

if free memory is only 1% there is no reasonable processing possible. and if the dll allocated the memory there is no way to go on as long as there is no handle to the used memory in the dll (see comments above). even if you would provide a c++ function which could free the memory, it doesn't help if the memory used was a leak or if the memory to free still was in use.

Sara
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.