benjaminvr

Tests object[] en memory usage

Nov 27th, 2021
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Text;
  5.                    
  6. public static class MemInfo {
  7.     public static readonly string[] SizeSuffixes =
  8.                    { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  9.    
  10.     public static string SizeSuffix(Int64 value, int decimalPlaces = 1)
  11.     {
  12.         if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("Invalid aantal decimal plaatsen"); }
  13.         if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); }
  14.         if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
  15.        
  16.         int mag = (int)Math.Log(value, 1024);
  17.         decimal adjustedSize = (decimal)value / (1L << (mag * 10));
  18.  
  19.         if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
  20.         {
  21.             mag += 1;
  22.             adjustedSize /= 1024;
  23.         }
  24.  
  25.         return string.Format("{0:n" + decimalPlaces + "} {1}",
  26.             adjustedSize,
  27.             SizeSuffixes[mag]);
  28.     }
  29.    
  30.     public static void GiveMemoryUsage(string msg=""){
  31.         Process proc = Process.GetCurrentProcess();
  32.         Console.WriteLine("_____________________________________________________________________________________________________");
  33.         Console.WriteLine($"_____| {msg} |_____");
  34.         Console.WriteLine("Maximum gealloceerd geheugen: "+ SizeSuffix(proc.PeakVirtualMemorySize64));
  35.         Console.WriteLine("Huidig gealloceerd geheugen: "+SizeSuffix(proc.PrivateMemorySize64));
  36.         int pct = (int)Math.Round((double)(100 * proc.PrivateMemorySize64) / proc.PeakVirtualMemorySize64);
  37.         Console.WriteLine($"Percentage gealloceerd: ~ {pct}%");
  38.         Console.WriteLine("_____________________________________________________________________________________________________");
  39.         proc.Dispose();
  40.     }
  41. }
  42.  
  43. public class Program
  44. {
  45.  
  46.    
  47.     public static void Main()
  48.     {
  49.         MemInfo.GiveMemoryUsage("Start van programma");
  50.        
  51.         object[] test5 = new object[20];
  52.        
  53.         MemInfo.GiveMemoryUsage("Na aanmaak van object array met 20 vrije plaatsen (= 8 byte per plekje)");
  54.        
  55.         string mb = new string('.', 1048576);
  56.         int aantalBytes = Encoding.Default.GetBytes(mb).Length;
  57.         Console.WriteLine("1MB string:\n\tAantal bytes = "+aantalBytes);
  58.         Console.WriteLine("\t          MB = "+MemInfo.SizeSuffix(aantalBytes)+"\n"); // de string is 1MB
  59.        
  60.         foreach(int i in Enumerable.Range(0, 10)){
  61.             char[] x = new char[mb.Length];     // char array met lengte mb string
  62.             mb.CopyTo(x);                       // de mb string als de char array
  63.             string hardCopy = new string(x);    // hard copy adhv de char array
  64.             if(i == 1) { Console.WriteLine("Controle char[] grootte: "+Encoding.Default.GetBytes(x).Length
  65.                                            +"\nControle string grootte: "+Encoding.Default.GetBytes(hardCopy).Length ); } // controle van lengtes
  66.             test5[i] = hardCopy;            // char array naar string en toekennen aan plekje in object[]
  67.             x = new char[0];                // tijdelijke char array en string resetten
  68.             hardCopy = String.Empty;
  69.             if(i == 1) { Console.WriteLine("Controle na ledigen char[] grootte: "+Encoding.Default.GetBytes(x).Length
  70.                                            +"\nControle na ledigen string grootte: "+Encoding.Default.GetBytes(hardCopy).Length );} // opnieuw controle van lengtes
  71.         }
  72.        
  73.         // 65.7 MB --> 119.4 MB = +53.7MB
  74.         MemInfo.GiveMemoryUsage("Na het inserten van 10 strings dmv char[] naar new string, elk 1 MB, in de object array");
  75.        
  76.         foreach(int i in Enumerable.Range(10,10)){
  77.             test5[i] = String.Copy(mb);
  78.         }
  79.        
  80.         // 119.4 MB --> 137.7 MB = +18,3MB
  81.         MemInfo.GiveMemoryUsage("Na het inserten van 10 extra strings dmv String.Copy(string), elk 1 MB, in de object array");
  82.        
  83.         // Controle van waarden en hun groottes
  84.         Console.WriteLine("Controle van item groottes in object[]");
  85.         foreach(int i in Enumerable.Range(0,20)){
  86.             Console.WriteLine($"\tItem {i} = {Encoding.Default.GetBytes(test5[i] as string).Length} bytes");
  87.         }
  88.        
  89.        
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment