Use. NET Memory Profiler to track Memory usage of. net Applications-Basic Applications
Author: Xiao Bo
The. net Framework claims that memory leakage will never occur because it introduces the memory reclaim mechanism. However, in practical applications, we often allocate objects but do not release references pointing to the objects, so that the objects will never be released. The most common case is that an event handler is added to an object, but this function is not removed from the event handler when the object is no longer used. In addition, if you allocate an unmanaged memory instead of manually releasing it, GC is powerless. Therefore, it is very important to locate the defects in the program design when the. net application detects Memory leakage and how to track the memory usage of the application. This article describes how to use. NET Memory Profiler to track Memory leakage of. net applications, which provides a solution for locating Memory problems of. net applications.
. NET Memory Profiler is a powerful. net Memory tracking and optimization tool. This tool currently supports memory tracking for four. net applications.
- Basic applications such as winform and console application
- ASP.net Application
- WPF Application
- Window Service
This article describes how to use the tool to track the memory of basic. net Applications by tracking the following three types of memory. Three types of memory are available:
- Managed memory
- Thread-managed memory
- Unmanaged memory
Before you start, you must create an environment.
I used. NET Memory Profiler V3.1.307 for testing. After installation, you need to create a new project, because we need to test
. Net basic application, so select Standalone application. Click next when creating a project, and enter the path and parameters of the. net application to be tested.
Then press finish. The project is created.
The test program is compiled and compiled to generate the console application testmemorysize.exe.
The Code is as follows:
The main program waits for user input. The input m, t, and u respectively increase the managed memory, create a thread that automatically increases the managed memory, and increase the unmanaged memory.
Enter d to release the managed memory object created by the main thread.
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace TestMemorySize
{
Class Program
{
Static void Main (string [] args)
{
MemoryInc memoryInc = new MemoryInc ();
While (true)
{
Long memorysize = System. Diagnostics. Process. GetCurrentProcess (). PagedMemorySize64;
Console. WriteLine (string. Format ("PagedMemorySize: {0} MB", memorysize/(1024*1024 )));
Console. WriteLine (string. Format ("ManagedMemIncTimes: {0}", memoryInc. ManagedMemIncTimes ));
Console. WriteLine (string. Format ("UnmanagedMemIncTimes: {0}", memoryInc. UnmanagedMemIncTimes ));
String cmd = Console. ReadLine ();
Switch (cmd)
{
Case "d ":
MemoryInc = new MemoryInc ();
GC. Collect ();
Break;
Case "m ":
MemoryInc. IncManagedMemory ();
Break;
Case "u ":
MemoryInc. IncUnmanagedMemory ();
Break;
Case "t ":
MemoryLeakThread thread = new MemoryLeakThread ();
Break;
Case "l ":
Break;
}
}
}
}
}
MemoryInc is a class that adds hosted memory and unmanaged memory.
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. InteropServices;
Namespace TestMemorySize
{
Class MemoryInc
{
Int _ ManagedMemIncTimes = 0;
Int _ UnmanagedMemIncTimes = 0;
List <byte []> _ ManagedMemory = new List <byte []> ();
Pipeline list <IntPtr> _ UnmanagedMemory = new pipeline list <IntPtr> ();
/// <Summary>
/// Managed memory increase times
/// </Summary>
Public int ManagedMemIncTimes
{
Get
{
Return _ ManagedMemIncTimes;
}
}
/// <Summary>
/// Unmanaged memory increase times
/// </Summary>
Public int UnmanagedMemIncTimes
{
Get
{
Return _ UnmanagedMemIncTimes;
}
}
/// <Summary>
/// Increase managed memory
/// </Summary>
Public void IncManagedMemory ()
{
_ ManagedMemIncTimes ++;
_ ManagedMemory. Add (new byte [1024*1024 * _ ManagedMemIncTimes]);
}
/// <Summary>
/// Increase unmanaged memory
/// </Summary>
Public void IncUnmanagedMemory ()
{
_ UnmanagedMemIncTimes ++;
_ UnmanagedMemory. AddLast (Marshal. AllocCoTaskMem (1024*1024 * _ UnmanagedMemIncTimes ));
}
}
}
The MemoryLeakThread thread does not increase the memory usage by 1 MB in 30 seconds.
MemoryLeakThread
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Threading;
Namespace TestMemorySize
{
Class MemoryLeakThread
{
Thread _ Thread;
Byte [] _ Buf;
Int _ Times = 0;
Private void ThreadProc ()
{
While (true)
{
_ Times ++;
_ Buf = new byte [_ Times * 1024x1024];
Thread. Sleep (30*1000 );
}
}
Public MemoryLeakThread ()
{
_ Thread = new Thread (new ThreadStart (ThreadProc ));
_ Thread. IsBackground = true;
_ Thread. Start ();
}
}
}
Ready for use.
1. Tracking of managed memory
Select Profiler-> Start to Start testmemorysize.exe from the menu, enter m and press Enter. This is the managed memory allocated with 1 MB.
Select Profiler-> Collect Heap Shapshot in the menu. This shows all the objects in the Heap.
From this interface, we can see that although the list of objects is listed, There is only information such as the type and size, but there is no object name and allocation process.
Information, so how to locate that memory is not released? Don't worry,. NET Memory Profiler is quite powerful. Let's continue.
Forward.
Double-click the selected object and enter the details of the heap occupied by the object.
Double-click the selected row to view the Object Name and stack allocation. Are you very excited? Finally, find out which guy is playing tricks.
2. Tracking of managed memory created in the thread
The managed memory tracking methods created in the thread are basically the same as those described in section 1st. Start testmemorysize.exe, enter t, and press enter to create
The thread that eats memory. The steps below are the same.
3. Tracking of unmanaged memory
To track the unmanaged memory, you need to make a setting: select view> Project Property Pages in the menu, and click set.
After setting, start testmemorysize.exe, enter u, and press enter to create 1 MB of unmanaged memory. The steps below are the same.
The name of the object cannot be seen in the unmanaged memory, but the memory application process can be seen, which is helpful for locating memory problems.
Now, enter m and press enter to create 1 MB of managed memory and d and press enter. Then we can find that the managed memory applied for by the memoryInc object has been released,
However, the unmanaged memory still exists, and the internal memory is leaked!
This tool can also help us calculate the actual memory size occupied by the managed object in the heap, which is also a very practical function, we can find the actual occupied size
It is slightly larger than the size we designed, because the classes we designed and their members are inherited from some base classes, which occupy some memory.
This article describes how to track the memory problems of basic. net applications. If you have time, thank you for tracking the memory problems of ASP. NET applications.
This article was about to be published in the morning. It was almost finished, and IE crashed! Crazy!
I wrote it again in the afternoon. It was depressing.