相关疑难解决方法(0)

如何在C#中分配超过MaxInteger字节的内存

我希望分配超过MaxInteger字节的内存.

Marshall.AllocHGlobal()需要一个整数 - 所以我不能使用它.还有另外一种方法吗?

更新

我将平台更改为x64,然后运行下面的代码.

myp似乎有正确的长度:约3.0G.但固执地"缓冲"最高可达2.1G.

知道为什么吗?

    var fileStream = new FileStream(
          "C:\\big.BC2",
          FileMode.Open,
          FileAccess.Read,
          FileShare.Read,
          16 * 1024,
          FileOptions.SequentialScan);
    Int64 length = fileStream.Length;
    Console.WriteLine(length);
    Console.WriteLine(Int64.MaxValue);
    IntPtr myp = new IntPtr(length);
    //IntPtr buffer = Marshal.AllocHGlobal(myp);
    IntPtr buffer = VirtualAllocEx(
        Process.GetCurrentProcess().Handle,
        IntPtr.Zero,
        new IntPtr(length),
        AllocationType.Commit | AllocationType.Reserve,
        MemoryProtection.ReadWrite);
    unsafe
    {
        byte* pBytes = (byte*)myp.ToPointer();
        var memoryStream = new UnmanagedMemoryStream(pBytes, (long)length, (long)length, FileAccess.ReadWrite);
        fileStream.CopyTo(memoryStream);
Run Code Online (Sandbox Code Playgroud)

c# memory-management

1
推荐指数
1
解决办法
2825
查看次数

标签 统计

c# ×1

memory-management ×1