OutOfMemory exception
This exception may be caused by the general language version when there is not enough memory to allocate for internal purposes or new instances of the object. To avoid this exception, avoid programming large methods that consume 64 or more kilobytes of memory.
Thus, one of the possible reasons may be the method in which you save your XML document, trying to allocate more than 64 KB.
To avoid corruption, the answer is "Matten" is good enough, and Jon skeet offers the same here .
But I want to add additional security checks using locks during this operation.
private static readonly object locker = new object(); public static void OnlyOneCallerAllowed() { lock (locker) { string tempFileName = System.IO.Path.GetTempFileName(); xmlDoc.Save(tempFileName); File.Move(original_file, original_file.old); File.Move(tempFileName,original_file); File.Delete( tempFileName); } }
source share