473,242 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

Re: Memory leak using pInvoke

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?
Jun 27 '08 #1
3 3177
Could you tell me why we should use 'out' keyword instead of 'ref' keyword?

"not_a_commie" wrote:
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?
Jun 27 '08 #2
On Tue, 24 Jun 2008 01:40:00 -0700, Victor
<Vi****@discussions.microsoft.comwrote:
Could you tell me why we should use 'out' keyword instead of 'ref'
keyword?
To the extent that your rising memory footprint is related to the
allocation of a BITMAPINFOHEADER object before calling the
VfwDecoder_GetOutputBitmapInfoHeader() function, using an "out" parameter
instead of "ref" allows you to avoid allocating the object, since it
wouldn't need to be initialized before passing to the function.

That said, I'm not really clear on the code example. The DllImport you
show is for VfwDecoder_DecompressNative, not
VfwDecoder_GetOutputBitmapInfoHeader, and you don't show us how you've
actually declared BITMAPINFOHEADER in C#. I'm relatively inexperienced
with p/invoke, so maybe it wouldn't help me understand your question
better anyway. But it could be useful to someone else with more
experience with respect to identifying anything about your code that might
be an issue.

All that said, the real bottom line here is that as long as you're dealing
with managed references to objects, the fact that your memory usage climbs
is not really a problem. The framework manages its use of actual
allocated memory from the OS independent of your own use of managed
memory, and it's not uncommon to see the process memory allocations be
larger than your application's actual use of managed memory.

Pete
Jun 27 '08 #3
Unfortunatley I don't have a solution to the OPs problem, but I have a
similar problem and have done some troubleshooting on it. I'm in way over my
head here so please don't flame me if I state the obvious.
I have written a very small test program.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace leakapp {
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal class DataContainer {
public Int32 m_arrayBindIndex = 12;
}

class Program {

static void Main(string[] args) {
Console.WriteLine("Begin...");
Console.ReadLine();
DataContainer ctx = new DataContainer();
long count = 100000;
for (long q = 0; q < 5; q++) {
for (long i = 0; i < count; i++) {
LeakClass(ref ctx);
}
Console.WriteLine("Outer: " + q.ToString());
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}

Console.WriteLine("Done");
Console.ReadLine();
}
/// <summary>
/// Call the dumy dll with a reference to the DataContainer.
/// </summary>
[DllImport("foo.dll", EntryPoint = "LeakClass", CallingConvention =
CallingConvention.Cdecl)]
public static extern int LeakClass(ref DataContainer cl);

}
}

foo.h:
typedef struct _DataContainer {
int m_arrayBindIndex;
} DataContainer;

extern "C" {
// Declaration of the compression method
__declspec(dllexport) void LeakClass(DataContainer **flum);
};

foo.cpp:
// This is the main DLL file.
#include <objbase.h>
#include "foo.h"

void LeakClass(DataContainer ** flum) {
}
(Don't comment on the usefullness of the code, it is only an example, as is
probably obvious from the naming etc. :-))

Running the program and attaching DebugDiag will reveal that on every call
to the unmanaged method there is a leak of 4 bytes. The leak will be on the
default process heap due to a call to CoTaskMemAlloc (used by the
interop-marshaler). DebugDiag reports that the allocations come from
mscorwks!BlittablePtrMarshalerBase::ConvertSpaceCL RToNative+23 . I can even
remove the only field in the class DataContainer passed to the unmanaged
method, this will result in a leak of 1 byte per call. The leak is not in
managed memory, but just to avoid all replys about "The GC hasn't run yet.."
etc. I included the GC.Collect()-sequence.
The above problem arises in our production servers. These small memory leaks
will fragment the default process heap (CoTaskMemAlloc heap) eventualy
crashing the app.
So, am I doing something stupid here? I have read about pinvoke and interop
marshaling but I can't find anything that is wrong with the code. Changing
the managed definition to a struct solves the problem. I would still like to
know what is wrong with the code I posted above.
Hope someone has some insight into thi.
/Erik
"Peter Duniho" wrote:
On Tue, 24 Jun 2008 01:40:00 -0700, Victor
<Vi****@discussions.microsoft.comwrote:
Could you tell me why we should use 'out' keyword instead of 'ref'
keyword?

To the extent that your rising memory footprint is related to the
allocation of a BITMAPINFOHEADER object before calling the
VfwDecoder_GetOutputBitmapInfoHeader() function, using an "out" parameter
instead of "ref" allows you to avoid allocating the object, since it
wouldn't need to be initialized before passing to the function.

That said, I'm not really clear on the code example. The DllImport you
show is for VfwDecoder_DecompressNative, not
VfwDecoder_GetOutputBitmapInfoHeader, and you don't show us how you've
actually declared BITMAPINFOHEADER in C#. I'm relatively inexperienced
with p/invoke, so maybe it wouldn't help me understand your question
better anyway. But it could be useful to someone else with more
experience with respect to identifying anything about your code that might
be an issue.

All that said, the real bottom line here is that as long as you're dealing
with managed references to objects, the fact that your memory usage climbs
is not really a problem. The framework manages its use of actual
allocated memory from the OS independent of your own use of managed
memory, and it's not uncommon to see the process memory allocations be
larger than your application's actual use of managed memory.

Pete
Jul 1 '08 #4

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

Similar topics

3
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
7
by: Rich Denis | last post by:
Hello, I have been trying to solve a mysterious memory leak problem and was hoping that you could help me out on my stuck point. First a bit of background. We have two app servers in an app...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
7
by: Ragnar Agustsson | last post by:
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...
4
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. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.