Yes, it will crash. This is the sample code I tried:
class Program
{
static byte[] arr = new byte[1024 * 1024 * 1024];
static byte[] arr2 = new byte[1024 * 1024 * 1024];
static byte[] arr3 = new byte[1024 * 1024 * 1024];
static byte[] arr4 = new byte[1024 * 1024 * 1024];
static void Main(string[] args)
{
Console.ReadKey();
}
}
This is a rather primitive test program. Unless the full amount of data is strongly reachable at once it might already be partially reclaimed by GC and therefore not crash.
You can also make them local. However, if you make a Release Build (with code optimisation) the following bit works like a charm, since unused arrays (or generally variables) may be optimised away:
static void Main(string[] args)
{
byte[] arr = new byte[1024 * 1024 * 1024];
byte[] arr2 = new byte[1024 * 1024 * 1024];
byte[] arr3 = new byte[1024 * 1024 * 1024];
byte[] arr4 = new byte[1024 * 1024 * 1024];
Console.ReadKey();
}
In a Debug Build (no code optimisation) the above bit crashes with an OutOfMemoryException
too.
Fields in contrast cannot be just left away of course. Therefore the first sample crashes no matter if the code is optimised by the compiler or not.