VS2019 CPU/memory diagnostic function

VS Code memory/CPU usage diagnostics

During the running of the code, sometimes there will be memory leaks, high memory/CPU usage, etc. These situations will greatly affect the running efficiency and robustness of the code, so it is necessary to diagnose and optimize the code. Here, use the code diagnostic tool that comes with VS2019 for detection.
The specific operation method is

  1. First add a breakpoint to the part of the code to be diagnosed or let the program run completely (preferably able to loop)

  2. Run the code (here, take drawing a triangle as an example, the screen is constantly being refreshed) and you can see the running status of the memory/CPU
    insert image description here

  3. Here we take the CPU usage as an example, and the memory usage is the same. Select a section or the entire running process and click the CPU usage, and then click All interrupts to view the CPU usage
    insert image description here

  4. The CPU usage can be roughly seen from the pie chart, and then click on the external code, select the concerned function from the external code to check the specific situation of the internal usage of the code, and optimize the code as needed.

Memory Leak Diagnostics

Allocating memory on the heap (new or malloc) but not releasing it (delete or free) will cause memory leaks. In order to avoid problems caused by forgetting to release, we can perform memory diagnosis on the code. The method is similar to CPU diagnosis. The main difference is that we need Use the memory usage here to view the specific situation.
Just write a bunch of code here, and make sure it is not released (memory leak)

//Test.h
#pragma once
#include<iostream>
using namespace std;
class Test
{
    
    
public:
	Test();
	Test(int a, int b);
	Test(const Test& t);
	~Test();
	void func();
	int m_a, m_b, m_c[100];//需要分配408个字节
};

#include "Test.h"

Test::Test()
{
    
    
	cout << "调用无参构造函数" << endl;
}

Test::Test(int a, int b)
{
    
    
	m_a = a;
	m_b = b;
	cout << "调用构造函数" << endl;
}

Test::Test(const Test& t)
{
    
    
	m_a = t.m_a;
	m_b = t.m_b;
	cout << "调用拷贝构造" << endl;
}

Test::~Test()
{
    
    
	cout << "调用析构函数" << endl;
}

void Test::func()
{
    
    
	
}
//主函数
#include"Test.h"
void func()
{
    
    

	
	//delete t;
}
void main()
{
    
    
	
	for (int i = 0; i < 100; i++)
	{
    
    
		
		Test* t0 = new Test(1,2);//内存泄漏 100*408
	}
	for (int i = 0; i < 1000; i++)
	{
    
    
		Test* t0 = new Test();
		Test* t1 = new Test();//t1内存泄漏1000*408
		delete t0;//t0释放了
	}
	system("pause");
}

Click Memory Usage-"Intercept Snapshot-"Allocation Difference Value-"There are 1100 leaked pointers on the left, and the leaked memory 1100*408=448800 Double-click 1100 to enter the allocated memory
insert image description here
address
insert image description here
Double-click the address to jump directly to the memory leak, or Pretty quick.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_44478077/article/details/125313157