300 MB XML likely encoding UTF-8 -> 600 MB UTF-16 string
ReadToEnd uses a StringBuilder internally, it returns sb.ToString().
That alone results in at least 1200 MB being allocated at one point. There is also a bit of buffering and the memory used to convert from UTF-8 to UTF-16.
If you are doing this in ASP.Net the application pool has usually a default memory limit - usually resulting in the pool being recycled.
If you are running under a 32-bit os those 8 GB will not help at all, and even if you are running under a 64-bit os, you have to make sure that the process is executed as a 64-bit process. Running it under Visual Studio defaults to 32-bit execution ...
I would try another method of processing a 300 MB xml file.
A working SAX based approach would probably be better - I haven't tested this, but it is perhaps worth a try:
Towards a Declarative SAX Framework : Part 1 - A Simple SAX-to-C#-Mapping[
^]
Best regards
Espen Harlinn