This a moot question, as the amount I was allocating was wrong. It was supposed to be 400MB not 400GB… Sorry! The answers are good though.
I am trying to unit test a method of mine which throws an OutOfMemoryException if a large file is uploaded.
My problem is my unit test is also throwing this exception (for the same reasons I guess).
According to Task Manager, if I am reading the information correctly, I do have enough RAM so am not really sure why my unit test is failing in this way.

Here is the respective unit test code:
var data = new List<byte>();
for(UInt64 i = 0; i < 400ul * 1024ul * 1024ul * 1024ul; i += 65536ul)
{
data.AddRange(new byte[65536]); // <-- throws exception here when i = 536870912
}
this.UploadedFileData = data.ToArray();
I tried the following before the above code:
var data = new byte[400ul * 1024ul * 1024ul * 1024ul];
This though, results in the error: Arithmetic operation resulted in an overflow.
Are you on a 32 bit machine?
From C# Increase Heap Size – Is It Possible
It is possible that you need to do the below
From http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
As David mentions, the code will require a lot of reallocations when it grows. To prevent performance costs and memory fragmentation, you should at the very least set
Capacityto an appropriate amount, if not rewrite the code.