Man*_*oon 1 c# memory-management
我希望分配超过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)
这在目前的主流硬件上是不可能的.内存缓冲区限制为2千兆字节,即使在64位计算机上也是如此.缓冲区的索引寻址仍然使用32位有符号偏移完成.技术上可以生成可以索引更多的机器代码,使用寄存器来存储偏移量,但这样做很昂贵并且会减慢所有数组索引,即使对于不大于2 GB的索引也是如此.
此外,您无法从32位进程可用的地址空间中获得大于约650MB的缓冲区.没有足够的连续内存页面可用,因为虚拟内存包含各种地址的代码和数据.
像IBM和Sun这样的公司出售可以做到这一点的硬件.
| 归档时间: |
|
| 查看次数: |
2825 次 |
| 最近记录: |