The Limitation On the Size of .Net Array

后端 未结 5 546
北荒
北荒 2020-11-28 13:32

I heard that there is a hard limit on the size of .Net Array. It is said that the maximum amount of memory that can be allocated to any single instance of an

相关标签:
5条回答
  • You will run into a practical limit first - it is pretty impossible to get a 2gb array allocated. Practical limits I have encountered are around the 800mb mark AT PROGRAM START - going down drastically after that.

    Anything larger than 64mb is a luck gamble on 32 bit - the large object heap is not defragmented, so you need 65mb free in one piece or allocation fails.

    Theoretical limits are:

    • usable memory, especially under 32 bit.
    • 32 bit number space for index (0 upward - no negative numbers for arrays UNLESS YOU PLAY SMART IN CREATION). You can create arrays allowing negative numbers, but not with C# standard syntax - only with reflection.
    • 2gb per object.

    But seriously, the practical implications are larger.

    For .NET 4.0.... consider using memory mapped files ;)

    0 讨论(0)
  • 2020-11-28 13:55

    That is correct. No single object can be larger than 2 GB.

    As with 32-bit Windows operating systems, there is a 2GB limit on the size of an object you can create while running a 64-bit managed application on a 64-bit Windows operating system.

    This question has additional details and some useful links: Single objects still limited to 2 GB in size in CLR 4.0?

    0 讨论(0)
  • 2020-11-28 13:58

    Hope this help: http://blogs.msdn.com/joshwil/archive/2005/08/10/450202.aspx

    i.e.

    1. It uses int as index, which has max value = 2,147,483,647 (2GB)
    2. Its by design. 2.
    0 讨论(0)
  • 2020-11-28 14:06

    I would have thought that the limit might be on the index. I thought that index used has to be an integer so anything bigger than integer wouldn't work unless they have some way around that. So that would be 4294967296 elements. Not sure if this is even half true. I would like to know the answer myself.

    EDIT: As tomtom pointed out, integer is usually signed unless they using a non signed integer. So half of 4294967296 or 2147483648 roughly.

    0 讨论(0)
  • 2020-11-28 14:08

    In versions of .NET prior to 4.5, the maximum object size is 2GB. From 4.5 onwards you can allocate larger objects if gcAllowVeryLargeObjects is enabled. Note that the limit for string is not affected, but "arrays" should cover "lists" too, since lists are backed by arrays.

    0 讨论(0)
提交回复
热议问题