如何ALLOC在C#中超过MaxInteger字节的内存 [英] How to alloc more than MaxInteger bytes of memory in C#

查看:289
本文介绍了如何ALLOC在C#中超过MaxInteger字节的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要分配的内存大于MaxInteger字节以上。



Marshall.AllocHGlobal()要求整 - 所以我不能用这个。有另一种方式?



更新



我改变了平台到x64,然后我跑到下面的代码



MYP似乎有合适的长度:约3.0G。但是,固执地缓冲在2.1G马克塞斯。



任何想法,为什么?



  VAR FILESTREAM =新的FileStream(
C:\\big.BC2,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
16 * 1024,
FileOptions.SequentialScan);
Int64的长度= fileStream.Length;
Console.WriteLine(长);
Console.WriteLine(Int64.MaxValue);
IntPtr的MYP =新的IntPtr(长度);
// IntPtr的缓冲= Marshal.AllocHGlobal(MYP);
IntPtr的缓冲= VirtualAllocEx来(
Process.GetCurrentProcess()处理,
IntPtr.Zero,
新的IntPtr(长度),
AllocationType.Commit |。AllocationType.Reserve ,
MemoryProtection.ReadWrite);
不安全
{
字节* pBytes =(BYTE *)myp.ToPointer();
变种的MemoryStream =新UnmanagedMemoryStream(pBytes,(长)长(长)长度,FileAccess.ReadWrite);
fileStream.CopyTo(MemoryStream的);


解决方案

这是不可能在目前的主流硬件。内存缓冲区被限制为2千兆字节,甚至在64位机器。索引寻址缓冲区仍然与32位有符号偏移量完成。这在技术上是可能产生机器代码可以索引越多,使用寄存器来存储偏移,但是这是昂贵的和减慢的所有的数组索引,甚至是不大于2 GB的那些



此外,你不能得到一个缓冲区比约650MB大出提供给32位进程的地址空间。有没有足够的可用连续内存页面,因为虚拟内存包含各种各样的地址代码和数据。



公司如IBM和Sun销售硬件可以做到这一点。


I wish to allocate more than MaxInteger bytes of memory.

Marshall.AllocHGlobal() expects an integer - so I cannot use this. Is there another way?

Update

I changed the platform to x64, and then I ran the code below.

myp appears to have the right length: about 3.0G. But stubbornly "buffer" maxes out at 2.1G.

Any idea why?

    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);

解决方案

That's not possible on current mainstream hardware. Memory buffers are restricted to 2 gigabytes, even on 64-bit machines. Indexed addressing of the buffer is still done with a 32-bit signed offset. It is technically possible to generate machine code that can index more, using a register to store the offset, but that's expensive and slows down all array indexing, even for the ones that aren't larger than 2 GB.

Furthermore, you can't get a buffer larger than about 650MB out of the address space available to a 32-bit process. There aren't enough contiguous memory pages available because virtual memory contains both code and data at various addresses.

Companies like IBM and Sun sell hardware that can do this.

这篇关于如何ALLOC在C#中超过MaxInteger字节的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆