VS2022 使用MFC CStringArray使用setatgrow添加元素,使用removeall()之后在使用setatgrow()添加元素,频繁操作内存一直增长,使用setsize()、FreeExtra()还是会增长,只是增长变得缓慢了。
{
m_arLabels.GetAt(i)->m_strTextArray.RemoveAll();
m_arLabels.GetAt(i)->m_strTextArray.FreeExtra();
for (int k = 0; k< 3; k++)
{
CString strValue = _T("");
if (k== 0)
strValue.Format(_T("%d"), i);
else if (k == 1)
strValue = CString(pszNames[i % 5]);
else
strValue.Format(_T("%d"), rand());
m_arLabels.GetAt(i)->m_strTextArray.Add(strValue);
}
解决内存增长的问题
SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1);
这个有没用?
可能问题不在你贴的这段代码里
代码里没有setSize和setatgrow啊,不清楚你说的现象是在什么地方添加的代码。
for (int i = 0; i < iRowNums; i++)
{
#if NORMAL_TEST
Label.m_iSerialNo = i;
Label.m_strText = pszNames[i % 5];
Label.m_Addr = rand();
#else
if (m_arLabels.GetSize() != 50000)
{
CLabelItem* Label = new CLabelItem();
Label->m_strTextArray.SetSize(10);
for (int j = 0; j < 3; j++)
{
CString strValue = _T("");
if (j == 0)
strValue.Format(_T("%d"), i);
else if (j == 1)
strValue = CString(pszNames[i % 5]);
else
strValue.Format(_T("%d"), rand());
Label->m_strTextArray.Add(strValue);
}
m_arLabels.SetAtGrow(i, Label);
}
else
{
m_arLabels.GetAt(i)->m_strTextArray.RemoveAll();
m_arLabels.GetAt(i)->m_strTextArray.FreeExtra();
SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1);
for (int k = 0; k < 3; k++)
{
CString strValue = _T("");
if (k == 0)
strValue.Format(_T("%d"), i);
else if (k == 1)
strValue = CString(pszNames[i % 5]);
else
strValue.Format(_T("%d"), rand());
m_arLabels.GetAt(i)->m_strTextArray.Add(strValue);
}
// m_arLabels.SetAtGrow(i, Label);
}
#endif
}
m_LabelCount = iRowNums;
m_VirtualList.SetItemCountEx(m_LabelCount);
m_VirtualList.Invalidate();
CString strTitle = _T("");
strTitle.Format(_T("耗时:\n[%ld]ms"), GetTickCount() - dBegin);
m_CostTimeText.SetWindowText(strTitle);
CLabelItem* Label = new CLabelItem();
分配内存后,也得记得释放啊。