0

I've got a problem with a C# application and a COM component allocating memory:

C# program calls a function in a COM DLL written in C++ which does matrix processing. The function allocates a lot of memory (around 800MB in eight 100MB chunks). This fails (malloc returns "bad allocation" when calling the function from C#.

If I run the same function from a C program, allocating the same amount of memory, then there's no problem allocating memory.

I've got 8GB RAM, Win7 x64 and there are plenty of free memory.

How to fix that it works to allocate memory when calling from the C# application? I tried to google it, but didn't really know what to search for. Searched for setting heap size etc, but that didn't give anything.

Feel a bit lost! All help are appreciated!

1
  • 1
    "The result is a plugin for Excel" is not a small detail you can leave out of a question. Commented Dec 3, 2013 at 16:04

1 Answer 1

3

Amount of physical memory (8 GB) is not the constraint that limits memory consumption of your application. Supposedly, you built 32-bit application which has a fundamental limit of 4 GB of directly addressable bytes. For historical reasons, the application not doing any magic has only half of this - 2 GB. This is where you allocate from, and this space is used for other needs. 100 MB chucks are large enough to reduce the effectively usable space because of memory/address fragmentation (you want not just 100 chunks, you request continuous ones).

The easiest solution here is to build 64-bit applications. The limits there are distant.

If you still want 32-bit code:

  • enable /LARGEADDRESSWARE on the hosting application binary to extend limit from 2 to 4 GB
  • use file mappings, which you can keep in physical memory with your data and map into metered address space on demand
  • allocate smaller chunks
Sign up to request clarification or add additional context in comments.

7 Comments

But it do work just fine with 32-bit C application. Why should it not work in 32 bit C#? The result is a plugin for Excel, and that it 32-bit anyway, if that matters.
OK, 32-bit C application will stumble on 13th instead of 8th. What is taking place is C# app has a larger footprint on the process address space.
You're 32-bit C application isn't starting up an instance of the CLR which alone uses memory to set up the initial managed heap and load the required libraries like your C# application is doing.
Note that your app will have to share address space with Excel as well, so you're having way less space to allocate from - and you will have to live with it, since you're playing away match here.
You can use out of process COM server, or otherwise split your addin so that it does its job in a separate process (which can be 64-bit, or dedicated 32-bit with still much more space than within Excel). This makes things more complicated, of course, but it is still doable and this is what I would perhaps do having this challenge in front of me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.