Bug 34382 - Marshal.AllocHGlobal out of memory exception with 64-bit ptr
Summary: Marshal.AllocHGlobal out of memory exception with 64-bit ptr
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: 4.0.0
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-09-29 05:27 UTC by Paolo
Modified: 2015-10-06 13:41 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Paolo 2015-09-29 05:27:07 UTC
I am trying to allocate unmanaged memory using Marshal.AllocHGlobal

When the size is greater than int.MaxValue (~2GB) I get this error message:
[ERROR] FATAL UNHANDLED EXCEPTION: System.OutOfMemoryException: Out of memory
 at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:AllocHGlobal (intptr)

This error occurs with mono Stable 4.0.4 and Alpha 4.2.1 on Ubuntu Server 14.04 in a virtual machine. Both the mono versions are using the 64-bit architecture. The code is compiled with VS2013 on a windows server 2012R2 machine and I made sure that the code was compiled in 64-bit checking that IntPtr.Size == 8.
Comment 1 Zoltan Varga 2015-10-06 11:09:13 UTC
On unix, AllocHGlobal uses the same allocator as malloc() does, and it looks like that can't allocate more than a certain amount of memory.
Comment 2 Paolo 2015-10-06 13:21:15 UTC
I did this test: on the same machine I used malloc() to allocate 30GB and it worked. I compiled the code using gcc.
Is it possible that AllocHGlobal uses a 32-bit version of malloc()?
Comment 3 Zoltan Varga 2015-10-06 13:41:54 UTC
Fixed by mono master 60cab7008ba24d189054d52bd39c8c9ee411e9db.