473,737 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clean up of memory leak in unmanaged code

Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson

Dec 18 '06 #1
7 15713
I don't think you will have much luck with the GC here, since these
problems arise with unmanaged code and that is exactly it, it's
unmanaged, so the GC won't deal with it.
Unfortunately I don't have a more elegant solution either, I usually
also use Marshal.Release ComObject when a memory leak occurs in
unmanaged libraries.
Depending on the situation you might be able to allocate the memory
yourself and free it when you are done, but I don't really consider
that to be more elegant or even more useful (unless you need to keep
the reference to the unmanaged object).

Sincerely,
Kevin Wienhold

Ragnar Agustsson schrieb:
Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson
Dec 18 '06 #2
Hi,

I think that your third option is the best way to go, you need to host the
leaking code in a different process (unfortunately using an AppDomain did
not worked, not "external enough").
--
Ignacio Machin
machin AT laceupsolutions com
"Ragnar Agustsson" <ra*****@gmail. comwrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.com.. .
Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson

Dec 18 '06 #3
Ragnar,

#3 is your best bet, to have the COM object run out of process, and then
have the process die when you are done with it.

You might get some help from hosting the COM object in COM+, as you
could have the app recycle should it consume/occupy more than a certain
amount of memory. Then, you just have to access it from .NET, and you
should be good.

Of course, you left out option #4, which is to get the source of the
component or a fix for the component in question. =)

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ragnar Agustsson" <ra*****@gmail. comwrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.com.. .
Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson

Dec 18 '06 #4
Kevin,

Your recommendation to use Marshal.Release ComObject for when a memory
leak occurs in unmanaged libraries is a bad one. There is no basis to do
this. If there is a memory leak in the unmanaged library, then that is not
going to be solved by the COM object being disposed of. If the COM object
is not managing the unmanaged memory correctly, then releasing it will not
do anything.

Rather, you are not managing the lifetime of the COM object correctly,
and are calling ReleaseComObjec t to account for that. Granted, you have to
do it at some point, but you need to know at WHAT point to do it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"KWienhold" <he******@trash mail.netwrote in message
news:11******** **************@ t46g2000cwa.goo glegroups.com.. .
>I don't think you will have much luck with the GC here, since these
problems arise with unmanaged code and that is exactly it, it's
unmanaged, so the GC won't deal with it.
Unfortunately I don't have a more elegant solution either, I usually
also use Marshal.Release ComObject when a memory leak occurs in
unmanaged libraries.
Depending on the situation you might be able to allocate the memory
yourself and free it when you are done, but I don't really consider
that to be more elegant or even more useful (unless you need to keep
the reference to the unmanaged object).

Sincerely,
Kevin Wienhold

Ragnar Agustsson schrieb:
>Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson

Dec 18 '06 #5
"Ragnar Agustsson" <ra*****@gmail. comwrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.com.. .
Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson

I second Nicholas advise about option 4, a piece of code that leaks memory is buggy code,
let the 3rd party fix it.

Willy.
Dec 18 '06 #6
Hi Nicholas

Acually Kevin's reccommendation works if the COM object is an out of
process object, i.e. COM server. Because it lives in a different
process and thus the memory is reclaimed when it is disposed of.

Best regards,
Ragnar

Nicholas Paldino [.NET/C# MVP] wrote:
Kevin,

Your recommendation to use Marshal.Release ComObject for when a memory
leak occurs in unmanaged libraries is a bad one. There is no basis to do
this. If there is a memory leak in the unmanaged library, then that is not
going to be solved by the COM object being disposed of. If the COM object
is not managing the unmanaged memory correctly, then releasing it will not
do anything.

Rather, you are not managing the lifetime of the COM object correctly,
and are calling ReleaseComObjec t to account for that. Granted, you have to
do it at some point, but you need to know at WHAT point to do it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"KWienhold" <he******@trash mail.netwrote in message
news:11******** **************@ t46g2000cwa.goo glegroups.com.. .
I don't think you will have much luck with the GC here, since these
problems arise with unmanaged code and that is exactly it, it's
unmanaged, so the GC won't deal with it.
Unfortunately I don't have a more elegant solution either, I usually
also use Marshal.Release ComObject when a memory leak occurs in
unmanaged libraries.
Depending on the situation you might be able to allocate the memory
yourself and free it when you are done, but I don't really consider
that to be more elegant or even more useful (unless you need to keep
the reference to the unmanaged object).

Sincerely,
Kevin Wienhold

Ragnar Agustsson schrieb:
Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson
Dec 18 '06 #7
I wish I had option 4, it would be the best one of course.

Thank you all for your replies, it confirmed my belief.

Best regards,
Ragnar

Willy Denoyette [MVP] wrote:
"Ragnar Agustsson" <ra*****@gmail. comwrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.com.. .
Hi all

I have been wandering about the best way to sandbox memory leaks in 3rd
party libraries when using them from the .Net framework.

I have a 3rd party library, written in C++, that leaks a lot of memory
but I still had to use it.

1.
After using DLLImport and seeing the memory leak I tried to load and
unload the library using the kernel's LoadLibrary and FreeLibrary. That
did not work as the leaked memory was not reclamed, not surprising as
my main .Net app's process was still alive.

2.
Then I read about AppDomain, that sounded pretty cool as it can unload
assemblies. So my hope was that the GC would be intelligent enough to
see the leaked memory and do a cleanup at that time. So I wrapped the
leaking C++ DLL in a C# assembly and loaded it, saw it leak memory,
then I unloaded the assembly and and saw it being unloaded. But the
memory was still missing...

3. Then I did the only thing I new would work, I wrote an out of
process COM object that I kill off with Marshal.Release ComObject to get
my memory back. I have thought about putting the logic into the COM
object itself, i.e. it would perform harakiri when consuming too much
memory or just with a certain interval or inactivity timeout. But this
approach does seem like a bit of a fudge to me...

So my question is:
Is there anyone out there who has found an elegant way of cleaning up
unmanaged memory leak using .Net?

Best regards,
Ragnar Agustsson


I second Nicholas advise about option 4, a piece of code that leaks memory is buggy code,
let the 3rd party fix it.

Willy.
Dec 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3276
by: Tom | last post by:
We have a VERY simple .NET C# Form Application, that has about a 23MB Memory Footprint. It starts a window runs a process and does a regular expression. I have done a GC.Collect to make sure that, no memory is lying around. GC reports only 84k of allocations. Starting 5-10 of this apps is going to start taking a considerable amount of memory. Is there a way to reduce this? Tom
0
3906
by: Frank Lopez | last post by:
Does anyone know if Microsoft generated a whitepaper on this topic? Does anyone know what the solution is? (meaning, eliminate the leak problem -- I am seeing three memory leaks from dllmodul.cpp(102) similar to what is mentioned below)... I am calling MFC as part of unmanaged code used by the managed code. +--------
0
2061
by: Frank Lopez | last post by:
My program structure is: 1. 2. 3. => manually does the crt-init and crt-terminate calls 4. -- this is accessed by the unmanaged C++ classes in (3) using LoadLibrary and FreeLibrary
15
2793
by: Chetan Raj | last post by:
Hi All, We have a web-application in asp.net that interacts with legacy code written in COM. The memory usage in aspnet_wp.exe increases every sec and never reduces. Using the .NET performance counters, we found that unmanaged memory was 90% of the total private bytes of aspnet_wp.exe. We suspected that the COM code has memory leaks. So we made it as a COM+ Service running as dllhost.exe. Surprisingly, there was no memory increase in...
9
425
by: Anton | last post by:
{Willy Skjveland} Hi, how can I trace a Memory leak in aspnet_wp.exe? {Rheena} One moment please while I search it for you. It may take me a few moments {Willy Skjveland} I need to find out which application and which dll and asmx page that cause the problem. {Rheena} May I know what operating system you are using?
4
13805
by: O.B. | last post by:
I've got this C# .NET 2005 application that has some unmanaged code. At random times, I get: An unhandled expection of type 'System.AccessViolationException' occurred in Unknown Module. Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. I have enabled "unmanaged code debugging" and when the error occurs,
25
3018
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class MyClass:IDisposable
22
9356
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
3
3234
by: not_a_commie | last post by:
The CLR won't garbage collect until it needs to. You should see the memory usage climb for some time before stabilizing. Can you change your declaration to use the 'out' keyword rather than a 'ref' keyword?
0
8965
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9466
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9202
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8202
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3278
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2191
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.