This question already has answers here:
What is the Maximum Size that an Array can hold?
(6 answers)
Closed 4 years ago.
byte[] buffer2 = new byte[4294743227]; // string with System.OverflowException
The number 4294743227 is uint.
Why do i get exception?
According to this question, the maximum size of an array is System.Int32.MaxValue, which is 2,147,483,647.
See also the documentation on System.Array:
By default, the maximum size of an Array is 2 gigabytes (GB). In a
64-bit environment, you can avoid the size restriction by setting the
enabled attribute of the gcAllowVeryLargeObjects configuration element
to true in the run-time environment. However, the array will still be
limited to a total of 4 billion elements, and to a maximum index of
0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and
arrays of single-byte structures).
Related
This question already has answers here:
Why .Net dictionaries resize to prime numbers?
(3 answers)
.NET ConcurrentDictionary initial capacity set to arbitrary prime number rather than expected capacity in MSDN example documentation. Why?
(1 answer)
Closed 4 months ago.
I was reading the source code of the dictionary and came across the resize method. I saw the formula temp = (currentSize * 2), currentSize = GetNextPrimalNumber(temp). Why we must to set primal number as a dictionary size? https://referencesource.microsoft.com/#mscorlib/system/collections/generic/dictionary.cs,440
Can somebody explain why we should use primal numbers as a size of dictionary? Thanks a lot !
This question already has answers here:
What is the Maximum Size that an Array can hold?
(6 answers)
Closed 4 years ago.
I have some folder that might contain a lot of files, I want to know what's the maximum size that string array can hold, I mean how many file names the array:
string[] files=Directory.GetFiles(#"c:\Dir\"); can hold?
Note that I'm asking about string array, not something else please.
Array Class
By default, the maximum size of an Array is 2 gigabytes (GB). In a
64-bit environment, you can avoid the size restriction by setting the
enabled attribute of the gcAllowVeryLargeObjects configuration element
to true in the run-time environment. However, the array will still be
limited to a total of 4 billion elements, and to a maximum index of
0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and
arrays of single-byte structures).
Very useful comment by Ňuf
But is should be noted that strings themself do not count towards the
2GB size limit, because the array contains only references to these
strings. So the maximal number of elements in string array is approx.
500M in 32bit process and 2G in 64bit process. Also this limit only
applies to .NET CLR, other implementations may have different limits
(e.g. Mono on 64bit supports even larger arrays with
–enable-big-arrays option)
This question already has answers here:
What is the maximum possible length of a .NET string?
(8 answers)
Closed 4 years ago.
I know in C# you can do int.MaxValue or long.MaxValue to get the maximum for these two types, I was wondering if there are similar ways to get the maximum length for the string.
I don't see it here.
Also, I am aware that there are already questions being asked on max string length. Max string length in C#
I am asking if there is an existing function that comes in handy where you can use it to set the condition when and when not to truncate your string to avoid program crashing.
Or what is the normal approach?
E.g. Maybe something as easy as Max(string.length)?
No. In the same way that there is no known method to find the max length of an array you can allocate. Until you try to allocate the memory, you can't know if there is enough virtual address space and enough physical memory to contain it. Theorically you could VirtualAlloc (Windows API) greater and greater memory blocks until it fails, deallocate the block and then try to allocate the same amount of memory in .NET, knowing that the memory is there so the .NET should be able to allocate it.
Note that this is true at 32 bits... I haven't ever seen an out-of-memory error at 64 bits.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Count the number of set bits in an integer
Best algorithm to count the number of set bits in a 32-bit integer?
That's an exam question and that is all I have - "Count the number of bits that are "on" in a byte" "On" means 1, I assume. Do I need to create a BitArray, randomly populate it and then iterate through it or is there a different way?
Using BitArray might be efficient but you could also do
byte b = ... ;
int count = Convert.ToString(b,2).ToCharArray().Count(c => c=='1');
Is this a interview question?
For a byte the fastest way would be to pre-compute an array such that a[i] = number of bits in i - the memory overhead is negligible.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the maximum possible length of a .NET string?
Is there limit for a C# string to hold data?
Strings cannot have more than 231 characters, since String.Length is a 32-bit integer.
They're also limited by available memory.
string.Length is int, so string can contain Int.MaxInt bytes - 2,147,483,647