c# – WorkingSetControl / RamControl: Displaying the Working Set size / current RAM usage of an application
English
Did you ever want to display the Working Set size / current RAM usage of your c# application? If the answer to this question is ‘yes’, here is your solution. If the answer is ‘no’ you still might find some usable code snippets in the control.
Deutsch
Es ist relativ einfach die aktuelle Speicherbelegung in einem c# Programm herauszufinden. Ich habe dies Funktionalität in ein kleinen Control verpackt.
Screenshot des Controls:
![]()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | using System; using System.Collections.Generic; using System.Windows.Forms; using System.Threading; using System.Diagnostics; using System.Drawing; namespace MyProject { /// <summary> /// Control to display the current WorkingSet size. Aka "RamControl". Just call Start to start the monitoring. /// </summary> class WorkingSetControl : Control { Thread t1; bool active = false; public WorkingSetControl() { SetStyle(ControlStyles.OptimizedDoubleBuffer, true); } /// <summary> /// Start displaying the current WorkingSet size. If you don't call Start you won't see a thing! /// </summary> internal void Start() { // start only if not started yet if (t1 == null) { // although we could use a lambda expression to create this thread don't use one to be .NET 2.0 compatible. t1 = new Thread(run); // if you *HAVE* to use a lambda expression, fine have fun with your cryptic code: /* t1 = new Thread(() => { while (active) { Invalidate(); Thread.Sleep(100); } }); */ // set active to true, so our run method does actually run in loops 'n loops 'n loops until active is set to false ==> see Dispose. active = true; t1.Start(); } } void run() { while (active) { Invalidate(); Thread.Sleep(100); } } /// <summary> /// Get and draw the WorkingSet size /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { string ram = "xxx"; // Only get the WorkingSet size if the control is in active mode. // This prevents the control displaying the WorkingSet size of Visual Studio in the designer (which works actually, but is somehow confusing). if (active) { long lngSessMemory = Process.GetCurrentProcess().WorkingSet64; ram = (Convert.ToDouble(lngSessMemory) / 1000 / 1000).ToString("0.00"); } // do the grphics stuff e.Graphics.Clear(BackColor); e.Graphics.DrawString("WorkingSet: " + ram + " MB", Font, new SolidBrush(ForeColor), 0, 0); } /// <summary> /// Lets make sure our thread terminates at some point in time. /// </summary> /// <param name="disposing"></param> protected override void Dispose(bool disposing) { // I know we should wait for the thread to actually funish by using t1.Join() but that would slow down this methob by up to 100ms. // If you have any Problems with controls not disposing properly, let me know and send me a solution ;) active = false; base.Dispose(disposing); } } } |