C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Virtual Memory Upper Limit
This section provides a tutorial example on how to test virtual memory upper limit, which is close to 2 GB on 32-bit Windows systems.
The next test is to see how much more memory can be allocated to a single process on my Windows 7 system. To do this I modified the previous tutorial example program as:
// MemoryCrash.cs // Copyright (c) 2010 HerongYang.com. All Rights Reserved. using System; public class MemoryCrash { public static void Main() { long[][] list; list = GetMegaBytes(4*1024); } public static long[][] GetMegaBytes(long max) { long[][] list = new long[max][]; Console.WriteLine("Memory Usage: #/Data/Private/Virtual/WS"); for (int n=0; n<max; n++) { PrintUsage(n); list[n] = new long[1024*128]; } return list; } public static void PrintUsage(long n) { System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess(); Console.Write("{0} / ", n); Console.Write("{0} / ", n*1024*1024); Console.Write("{0} / ", proc.PrivateMemorySize64); Console.Write("{0} / ", proc.VirtualMemorySize64); Console.Write("{0} / ", proc.WorkingSet64); Console.WriteLine(); } }
I compiled it with .NET 4.6.1 C# compiler and got this output with the OutOfMemoryException error as expected:
C:\herong>set "NET=\windows\Microsoft.NET\Framework\v4.0.30319" C:\herong>%NET%\csc MemoryCrash.cs Microsoft (R) Visual C# Compiler version 4.6.1055.0 C:\herong>MemoryCrash Memory Usage: #/Data/Private/Virtual/WS 0 / 0 / 6332416 / 107507712 / 6909952 / 1 / 1048576 / 7585792 / 107507712 / 7208960 / 2 / 2097152 / 8765440 / 107507712 / 7393280 / 3 / 3145728 / 10010624 / 107507712 / 7589888 / ... 1708 / 1790967808 / 1833287680 / 2041081856 / 28565504 / 1709 / 1792016384 / 1834336256 / 2041081856 / 28573696 / 1710 / 1793064960 / 1835384832 / 2041081856 / 28577792 / Unhandled Exception: OutOfMemoryException.
But when I compiled it with .NET 4.0 C# compiler and ran it on my Windows XP system, I did not get the OutOfMemoryException error. Here is what happened:
C:\herong>\windows\Microsoft.NET\Framework\v4.0.30319\csc MemoryCrash.cs C:\herong>MemoryCrash.exe Memory Usage: #/Data/Private/Virtual/WS ... 1666 / 1746927616 / 1950216192 / 2055503872 / 191340544 / 1667 / 1747976192 / 1951395840 / 2055503872 / 191492096 / 1668 / 1749024768 / 1952575488 / 2055503872 / 191631360 / 1669 / 1750073344 / 1953755136 / 2055503872 / 191766528 / 1670 / 1751121920 / 1954803712 / 2055503872 / 191774720 / 1671 / 1752170496 / 1955983360 / 2055503872 / 191909888 / 1672 / 1753219072 / 1957163008 / 2055503872 / 192049152 / 1673 / 1754267648 / 1958342656 / 2055503872 / 192184320 / 1674 / 1755316224 / 1959522304 / 2055503872 / 192323584 / 1675 / 1756364800 / 1960570880 / 2055503872 / 192327680 /
I was expecting an "out of memory" error after step 1675. But the following error message box showed up:
Visual Studio Just-In-Time Debugger - An unhandled win32 exception occured in MemroyCrash.exe [4784]. Just-In-Time debugging this exception failed with the following error: No installed debugger has Just-In-Time debugging enabled. In Visual Studio, Just-In-Time debugging can be enabled from Tools/Options/Debugging/Just-In-Time. Check the documentation index for "Just-in-time debugging, errors' for more information.
Do you know why? My guess is that the .NET 4 C# compiler I used was installed as part of the Visual C# 2010 Express Edition, not from the .NET 4 SDK.
If I compiled it with .NET 2 C# compiler from the .NET 2 SDK and got the "out of memory" error as expected:
C:\herong>\windows\Microsoft.NET\Framework\v2.0.50727\csc MemoryCrash.cs C:\herong>MemoryCrash.exe Memory Usage: #/Data/Private/Virtual/WS ... 1601 / 1678770176 / 1939230720 / 2039799808 / 242266112 / 1602 / 1679818752 / 1940410368 / 2039799808 / 242409472 / 1603 / 1680867328 / 1941590016 / 2039799808 / 242552832 / 1604 / 1681915904 / 1942769664 / 2039799808 / 242696192 / 1605 / 1682964480 / 1943949312 / 2039799808 / 242839552 / 1606 / 1684013056 / 1945128960 / 2039799808 / 242982912 / 1607 / 1685061632 / 1946308608 / 2039799808 / 243126272 / 1608 / 1686110208 / 1947488256 / 2039799808 / 243273728 / 1609 / 1687158784 / 1948667904 / 2039799808 / 243412992 / 1610 / 1688207360 / 1949847552 / 2039799808 / 243580928 / Unhandled Exception: OutOfMemoryException.
I also got the Windows standard crash message box:
MemoryCrash.exe has encountered a problem and needs to close. We are sorry for the inconvenience. If you were in the middle of something, the information you were working on might be lost. ...
Outputs from all tests show that the upper limit of virtual memory is close to 2 GB = 2147483648 bytes, which is the memory address limit of 32-bit Windows systems.
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
Using "Process" Class to Show Memory Usages
Private/Virtual Memory and Working Set
Footprints of Private and Shared Memories
Memory Report from Windows Task Manager
Memory Report from Performance Console
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation