Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- public static class MemInfo {
- public static readonly string[] SizeSuffixes =
- { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
- public static string SizeSuffix(Int64 value, int decimalPlaces = 1)
- {
- if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("Invalid aantal decimal plaatsen"); }
- if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); }
- if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
- int mag = (int)Math.Log(value, 1024);
- decimal adjustedSize = (decimal)value / (1L << (mag * 10));
- if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
- {
- mag += 1;
- adjustedSize /= 1024;
- }
- return string.Format("{0:n" + decimalPlaces + "} {1}",
- adjustedSize,
- SizeSuffixes[mag]);
- }
- public static void GiveMemoryUsage(string msg=""){
- Process proc = Process.GetCurrentProcess();
- Console.WriteLine("_____________________________________________________________________________________________________");
- Console.WriteLine($"_____| {msg} |_____");
- Console.WriteLine("Maximum gealloceerd geheugen: "+ SizeSuffix(proc.PeakVirtualMemorySize64));
- Console.WriteLine("Huidig gealloceerd geheugen: "+SizeSuffix(proc.PrivateMemorySize64));
- int pct = (int)Math.Round((double)(100 * proc.PrivateMemorySize64) / proc.PeakVirtualMemorySize64);
- Console.WriteLine($"Percentage gealloceerd: ~ {pct}%");
- Console.WriteLine("_____________________________________________________________________________________________________");
- proc.Dispose();
- }
- }
- public class Program
- {
- public static void Main()
- {
- MemInfo.GiveMemoryUsage("Start van programma");
- object[] test5 = new object[20];
- MemInfo.GiveMemoryUsage("Na aanmaak van object array met 20 vrije plaatsen (= 8 byte per plekje)");
- string mb = new string('.', 1048576);
- int aantalBytes = Encoding.Default.GetBytes(mb).Length;
- Console.WriteLine("1MB string:\n\tAantal bytes = "+aantalBytes);
- Console.WriteLine("\t MB = "+MemInfo.SizeSuffix(aantalBytes)+"\n"); // de string is 1MB
- foreach(int i in Enumerable.Range(0, 10)){
- char[] x = new char[mb.Length]; // char array met lengte mb string
- mb.CopyTo(x); // de mb string als de char array
- string hardCopy = new string(x); // hard copy adhv de char array
- if(i == 1) { Console.WriteLine("Controle char[] grootte: "+Encoding.Default.GetBytes(x).Length
- +"\nControle string grootte: "+Encoding.Default.GetBytes(hardCopy).Length ); } // controle van lengtes
- test5[i] = hardCopy; // char array naar string en toekennen aan plekje in object[]
- x = new char[0]; // tijdelijke char array en string resetten
- hardCopy = String.Empty;
- if(i == 1) { Console.WriteLine("Controle na ledigen char[] grootte: "+Encoding.Default.GetBytes(x).Length
- +"\nControle na ledigen string grootte: "+Encoding.Default.GetBytes(hardCopy).Length );} // opnieuw controle van lengtes
- }
- // 65.7 MB --> 119.4 MB = +53.7MB
- MemInfo.GiveMemoryUsage("Na het inserten van 10 strings dmv char[] naar new string, elk 1 MB, in de object array");
- foreach(int i in Enumerable.Range(10,10)){
- test5[i] = String.Copy(mb);
- }
- // 119.4 MB --> 137.7 MB = +18,3MB
- MemInfo.GiveMemoryUsage("Na het inserten van 10 extra strings dmv String.Copy(string), elk 1 MB, in de object array");
- // Controle van waarden en hun groottes
- Console.WriteLine("Controle van item groottes in object[]");
- foreach(int i in Enumerable.Range(0,20)){
- Console.WriteLine($"\tItem {i} = {Encoding.Default.GetBytes(test5[i] as string).Length} bytes");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment