C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Footprints of Private and Shared Memories
This section provides a tutorial example on how to estimate footprints of private memory and shared memory of C# applications.
In order to get a better understanding of private memory and virtual memory usages, I wrote another tutorial example program called, MemoryIncreased.cs, to see changes on memory usages when more private data is requested.
// MemoryIncreased.cs // Copyright (c) 2010 HerongYang.com. All Rights Reserved. using System; public class MemoryIncreased { public static void Main() { long[][] list; list = GetMegaBytes(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*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 C# compiler and got this output:
C:\herong>\windows\Microsoft.NET\Framework\v4.0.30319\csc MemoryIncreased.cs C:\herong>MemoryIncreased.exe Memory Usage: #/Data/Private/Virtual/WS 0 / 0 / 8753152 / 89227264 / 5799936 / 1 / 1048576 / 10072064 / 89227264 / 6225920 / 2 / 2097152 / 11448320 / 89227264 / 6508544 / 3 / 3145728 / 12627968 / 89227264 / 6774784 / 4 / 4194304 / 13807616 / 89227264 / 6909952 / 5 / 5242880 / 14987264 / 89227264 / 7049216 / 6 / 6291456 / 16035840 / 89227264 / 7065600 / 7 / 7340032 / 17215488 / 89227264 / 7204864 / 8 / 8388608 / 18395136 / 89227264 / 7344128 / 9 / 9437184 / 19574784 / 89227264 / 7479296 / 10 / 10485760 / 20885504 / 89227264 / 7757824 / 11 / 11534336 / 22196224 / 89227264 / 8032256 / 12 / 12582912 / 23506944 / 89227264 / 8323072 / 13 / 13631488 / 24883200 / 89227264 / 8605696 / 14 / 14680064 / 26193920 / 89227264 / 8888320 / 15 / 15728640 / 28790784 / 107249664 / 9216000 / 16 / 16777216 / 30101504 / 107249664 / 9502720 / 17 / 17825792 / 30756864 / 106594304 / 10043392 / 18 / 18874368 / 31936512 / 106594304 / 10182656 / 19 / 19922944 / 33116160 / 106594304 / 10317824 / 20 / 20971520 / 34295808 / 106594304 / 10457088 / 21 / 22020096 / 35475456 / 106594304 / 10592256 / 22 / 23068672 / 36655104 / 106594304 / 10731520 / 23 / 24117248 / 37834752 / 106594304 / 10870784 / 24 / 25165824 / 39014400 / 106594304 / 11005952 / 25 / 26214400 / 40062976 / 106594304 / 11059200 / 26 / 27262976 / 41111552 / 106594304 / 11063296 / 27 / 28311552 / 42160128 / 106594304 / 11071488 / 28 / 29360128 / 43208704 / 106594304 / 11075584 / 29 / 30408704 / 46858240 / 125796352 / 11239424 / 30 / 31457280 / 48037888 / 125796352 / 11374592 / ... 1014 / 1063256064 / 1228591104 / 1300529152 / 147308544 / 1015 / 1064304640 / 1229770752 / 1300529152 / 147447808 / 1016 / 1065353216 / 1230950400 / 1300529152 / 147603456 / 1017 / 1066401792 / 1232130048 / 1300529152 / 147738624 / 1018 / 1067450368 / 1233350656 / 1317306368 / 147886080 / 1019 / 1068498944 / 1234530304 / 1317306368 / 148021248 / 1020 / 1069547520 / 1235709952 / 1317306368 / 148160512 / 1021 / 1070596096 / 1236889600 / 1317306368 / 148295680 / 1022 / 1071644672 / 1238069248 / 1317306368 / 148434944 / 1023 / 1072693248 / 1239248896 / 1317306368 / 148574208 /
The output shows that:
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