Measure Memory Usage of .NET Applications
来源:互联网 发布:linux c 服务器开发 编辑:程序博客网 时间:2024/05/18 02:45
There are two main methods for measuring the memory usage of a .NET application, using the GC class or using System.Diagnostics
Retrieve Your App’s Memory Utilization using the GC class
The .NET Framework’s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to your app. The number not be exact right since some objects may not have been garbage collected yet. This, however does have the advantage of being able to inform you of the amount of memory a certain part of your app uses, as opposed to the entire process:
long memAvailable = GC.GetTotalMemory(false);Console.WriteLine(“Before allocations: {0:N0}”, memAvailable);int allocSizeObj = 40000000;byte[] bigArrayObj = new byte[allocSizeObj];memAvailable= GC.GetTotalMemory(false);Console.WriteLine(“After allocations: {0:N0}”, memAvailable);
The above code will output the following:
Before allocations: 651,064After allocations: 40,690,080
Retrieve Your App’s Memory Utilization using System.Diagnostics
You can also request the OS report on info about your process using the Process class in the System.Diagnostics namespace:
Process procObj = Process.GetCurrentProcess();Console.WriteLine(“Process Info: “+Environment.NewLine+“App's Private Memory Size : {0:N0}”+Environment.NewLine +“App's Virtual Memory Size : {1:N0}” + Environment.NewLine +“App's Working Set Size: {2:N0}” + Environment.NewLine +“App's Paged Memory Size: {3:N0}” + Environment.NewLine +“App's Paged System Memory Size: {4:N0}” + Environment.NewLine +“App's Non-paged System Memory Size: {5:N0}” + Environment.NewLine,procObj.PrivateMemorySize64,procObj.VirtualMemorySize64,procObj.WorkingSet64,procObj.PagedMemorySize64,procObj.PagedSystemMemorySize64,procObj.NonpagedSystemMemorySize64 );
Here’s the output:
Process Info:Private Memory Size: 75,935,744Virtual Memory Size: 590,348,288Working Set Size: 29,364,224Paged Memory Size: 75,935,744Paged System Memory Size: 317,152Non-paged System Memory Size: 37,388
These numbers are not necessarily intuitive, and you can consult a good operating system book, such as Windows Internals from Microsoft Press for more on how virtual memory works. Performance counters can also be used to track this as well as other info on your app or its environment.
There are two main methods for measuring the memory usage of a .NET application, using the GC class or using System.Diagnostics
Retrieve Your App’s Memory Utilization using the GC class
The .NET Framework’s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to your app. The number not be exact right since some objects may not have been garbage collected yet. This, however does have the advantage of being able to inform you of the amount of memory a certain part of your app uses, as opposed to the entire process:
long memAvailable = GC.GetTotalMemory(false);Console.WriteLine(“Before allocations: {0:N0}”, memAvailable);int allocSizeObj = 40000000;byte[] bigArrayObj = new byte[allocSizeObj];memAvailable= GC.GetTotalMemory(false);Console.WriteLine(“After allocations: {0:N0}”, memAvailable);
The above code will output the following:
Before allocations: 651,064After allocations: 40,690,080
Retrieve Your App’s Memory Utilization using System.Diagnostics
You can also request the OS report on info about your process using the Process class in the System.Diagnostics namespace:
Process procObj = Process.GetCurrentProcess();Console.WriteLine(“Process Info: “+Environment.NewLine+“App's Private Memory Size : {0:N0}”+Environment.NewLine +“App's Virtual Memory Size : {1:N0}” + Environment.NewLine +“App's Working Set Size: {2:N0}” + Environment.NewLine +“App's Paged Memory Size: {3:N0}” + Environment.NewLine +“App's Paged System Memory Size: {4:N0}” + Environment.NewLine +“App's Non-paged System Memory Size: {5:N0}” + Environment.NewLine,procObj.PrivateMemorySize64,procObj.VirtualMemorySize64,procObj.WorkingSet64,procObj.PagedMemorySize64,procObj.PagedSystemMemorySize64,procObj.NonpagedSystemMemorySize64 );
Here’s the output:
Process Info:Private Memory Size: 75,935,744Virtual Memory Size: 590,348,288Working Set Size: 29,364,224Paged Memory Size: 75,935,744Paged System Memory Size: 317,152Non-paged System Memory Size: 37,388
These numbers are not necessarily intuitive, and you can consult a good operating system book, such as Windows Internals from Microsoft Press for more on how virtual memory works. Performance counters can also be used to track this as well as other info on your app or its environment.
Reference from:http://www.csharphelp.com/2011/01/measure-memory-usage-of-net-applications/
Another related article can be found from: Measuring memory usage of object creation in C#
- Measure Memory Usage of .NET Applications
- How to measure memory usage of linux process
- Usage simple of shared memory
- Memory usage of Java objects: general guide
- Unit Of Measure(UOM) in Oracle Applications Inventory
- .NET Memory usage – A restaurant analogy
- Analyzing the memory usage of your Android application
- 内存泄露 Analyzing the memory usage of your Android application
- How to discover memory usage of my application in Android
- How to discover memory usage of my application in Android
- How to discover memory usage of my application in Android
- How to discover memory usage of my application in Android
- Show memory usage of all docker / lxc containers
- More on Memory Usage(.NET内存利用率)
- REDUCING MEMORY USAGE IN UNITY, C# AND .NET/MONO
- Optimizing the loading of AutoCAD .NET applications
- Tracing memory leaks in .NET applications with ANTS Profiler
- beyond virtual memory limits. Current usage: 142.3 MB of 1 GB physical memory used;
- SM30表格维护
- presentModalViewController和dismissModalViewControllerAnimated的使用总结
- mac os 下 adb识别各种非主流android手机
- 自己记录下string转stream和steam转string
- js屏蔽backspace按键
- Measure Memory Usage of .NET Applications
- oc学习笔记(一)基本语法
- 百度编辑器二次开发常用手记整理小结
- 这季节中,风是轻柔的
- 查询oracle表的信息(表,字段,约束,索引) 按列名+表名查询约束名称
- 【C++】变量(七) 参量
- string 转stream和stream转string
- ace nonblock
- eYdoae谑侍YBHOC