0

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.

3
  • @Fruchtzwerg it talks about int array, I'm asking about string array, please read carfuly Commented Sep 16, 2018 at 7:26
  • 1
    That makes no difference. Commented Sep 16, 2018 at 7:27
  • Array size limit is big enough that it's unlikely to to be a problem, but if you are still afraid, use Directory.EnumerateFiles() instead - it returns IEnumerable<string>. Commented Sep 16, 2018 at 8:08

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

That's also relevante for string array?
@CPU yes its relevant for arrays of any type
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)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.