user1309971
user1309971

Reputation: 617

C# calling COM fails to allocate memory

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!

Upvotes: 0

Views: 1266

Answers (1)

Roman Ryltsov
Roman Ryltsov

Reputation: 69632

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

Upvotes: 3

Related Questions