.Net 进程内存空间的有关问题
.Net 进程内存空间的问题
有一个使用Fortran编写的DLL,运行某个函数的时候需要申请大量的内存(约1.75 GB)。
我写了一个C程序调用这个函数,程序正常退出;但是写一个C#程序调用这个函数,却弹出错误“Insufficient Virtual Memory”。后来我又用VC++ .Net (Managed C++) 写了一个测试程序,程序正常退出。
C语言程序代码如下:
C#代码如下:
Managed C++的代码基本上类似于C代码,这里就不贴出来了。请问在一个C#进程中调用DLL时,到底发生了什么?为何只有C#程序会报错?如何避免这个问题?
------解决方案--------------------
参考:
Single objects still limited to 2 GB in size in CLR 4.0?
Is there a memory limit for a single .NET process
Insufficient memory error at only 1.7gb memory usage?
MemoryFailPoint always throws InsufficientMemoryException even when memroy is available
BigArray<T>, getting around the 2GB array size limit
CLR对象的最大限制是约2GB,甚至是在64位系统。
在.NET4.5开始,可以设置<gcAllowVeryLargeObjects> 元素
------解决方案--------------------
内存限制,,,
有一个使用Fortran编写的DLL,运行某个函数的时候需要申请大量的内存(约1.75 GB)。
我写了一个C程序调用这个函数,程序正常退出;但是写一个C#程序调用这个函数,却弹出错误“Insufficient Virtual Memory”。后来我又用VC++ .Net (Managed C++) 写了一个测试程序,程序正常退出。
C语言程序代码如下:
#include <windows.h>
#include <stdio.h>
typedef char (* InitialProc)();
int main(int argc, char *argv[])
{
HMODULE hLibrary = NULL;
InitialProc Initial = NULL;
hLibrary = LoadLibrary("river_dll.dll");
if (hLibrary == NULL)
{
OutputDebugString("Cannot load library\n");
return 0;
}
Initial = (InitialProc)GetProcAddress(hLibrary, "INITIAL");
if (Initial == NULL)
{
OutputDebugString("Cannot get proc address\n");
return 0;
}
Initial();
Sleep(3000);
OutputDebugString("Exit\n");
return 0;
}
C#代码如下:
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace dotnet_caller
{
class Program
{
[DllImport("river_dll.dll",
ExactSpelling = true,
EntryPoint = "INITIAL",
CallingConvention = CallingConvention.StdCall)]
public static extern bool initial();
static void Main(string[] args)
{
initial();
}
}
}
Managed C++的代码基本上类似于C代码,这里就不贴出来了。请问在一个C#进程中调用DLL时,到底发生了什么?为何只有C#程序会报错?如何避免这个问题?
------解决方案--------------------
参考:
Single objects still limited to 2 GB in size in CLR 4.0?
Is there a memory limit for a single .NET process
Insufficient memory error at only 1.7gb memory usage?
MemoryFailPoint always throws InsufficientMemoryException even when memroy is available
BigArray<T>, getting around the 2GB array size limit
CLR对象的最大限制是约2GB,甚至是在64位系统。
在.NET4.5开始,可以设置<gcAllowVeryLargeObjects> 元素
------解决方案--------------------
内存限制,,,