Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# memory usage

Tags:

c#

How I can get the actual memory used in my C# application?

  • Task Manager shows different metrics.
  • Process Explorer shows increased usage of private bytes.
  • Performance counter (perfmon.msc) showed different metrics
  • when I used .NET memory profiler, it showed most of the memory is garbage collected and only few Live bytes.

I do not know which to believe.

like image 889
jaks Avatar asked Sep 27 '10 10:09

jaks


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.


3 Answers

Memory usage is somewhat more complicated than displaying a single number or two. I suggest you take a look at Mark Russinovich's excellent post on the different kinds of counters in Windows.

.NET only complicates matters further. A .NET process is just another Windows process, so obviously it will have all the regular metrics, but in addition to that the CLR acts as a memory manager for the managed application. So depending on the point of view these numbers will vary.

The CLR effectively allocates and frees virtual memory in big chunks on behalf of the .NET application and then hands out bits of memory to the application as needed. So while your application may use very little memory at a given point in time this memory may or may not have been released to the OS.

On top of that the CLR itself uses memory to load IL, compile IL to native code, store all the type information and so forth. All of this adds to the memory footprint of the process.

If you want to know how much memory your managed application uses for data, the Bytes in all heaps counter is useful. Private bytes may be used as a somewhat rough estimate for the application's memory usage on the process level.

You may also want to check out these related questions:

Reducing memory usage of .NET applications?

How to detect where a Memory Leak is?

like image 92
Brian Rasmussen Avatar answered Oct 25 '22 16:10

Brian Rasmussen


If you are using VS 2010 you can use Visual Studio 2010 Profiler. This tool can create very informative reports for you.

like image 41
bahadir arslan Avatar answered Oct 25 '22 17:10

bahadir arslan


If you want to know approximately how many bytes are allocated on the GC heap (ignoring memory used by the runtime, the JIT compiler, etc.), you can call GC.GetTotalMemory. We've used this when tracking down memory leaks.

like image 25
Joe White Avatar answered Oct 25 '22 16:10

Joe White