将文本设置为大量数据后,Edit Control 的空闲堆?
Free Heap of Edit Control after setting the text to a large amount of data?
我在提取大量数据到 CEditView
然后设置回少量数据后,在任务管理器中查看我的进程。我注意到提交大小保持很大。然后我使用 VMMMap 也看到了它,所以我在 VS2017 诊断工具中做了“内存使用情况”。我看到它最终来自 ::SetWindowText()
调用。所以这显然在堆上分配了一个大缓冲区,但是当我将它设置回少量时,分配保持很大。问题是,有没有办法让 Edit Control 释放它不需要的内存以减少文本量以减少提交的内存?比如说,我可以在设置新文本之前释放它,它会根据需要分配?
谢谢!!
// From within CEditView
// based on an answer RbMm using EM_GETHANDLE/EM_SETHANDLE and further
// from
// reduce size to 64K if larger than that and not needed
#define EDITctrlLimitSize 65536
// check if we should reduce size of buffer
HLOCAL horgmem = (HLOCAL) SendMessage(EM_GETHANDLE,0,0);
SIZE_T sizeused=LocalSize(horgmem);
int cbCh = sizeof(TCHAR) > 1 ? sizeof(TCHAR) : IsUsingComCtlV6() ? sizeof(WCHAR) : sizeof(char);
if (sizeused > EDITctrlLimitSize && (string.GetLength()*cbCh) < EDITctrlLimitSize) {
// EM_GETHANDLE says to not change the data, yet EM_SETHANDLE tells you to
// get the handle then LocalFree it, so why not just LocalReAlloc it and set
// again.
HLOCAL hnewmem=(HLOCAL) LocalReAlloc(horgmem, EDITctrlLimitSize, LMEM_MOVEABLE);
if (hnewmem) {
// zero full buffer because seems like good thing to do
LPVOID pnewmem=LocalLock(hnewmem);
if (pnewmem) {
memset(pnewmem, 0, EDITctrlLimitSize);
LocalUnlock(hnewmem);
}
// viola - process memory reduced.
SendMessage(EM_SETHANDLE, (WPARAM) hnewmem, 0);
}
}
我在提取大量数据到 CEditView
然后设置回少量数据后,在任务管理器中查看我的进程。我注意到提交大小保持很大。然后我使用 VMMMap 也看到了它,所以我在 VS2017 诊断工具中做了“内存使用情况”。我看到它最终来自 ::SetWindowText()
调用。所以这显然在堆上分配了一个大缓冲区,但是当我将它设置回少量时,分配保持很大。问题是,有没有办法让 Edit Control 释放它不需要的内存以减少文本量以减少提交的内存?比如说,我可以在设置新文本之前释放它,它会根据需要分配?
谢谢!!
// From within CEditView
// based on an answer RbMm using EM_GETHANDLE/EM_SETHANDLE and further
// from
// reduce size to 64K if larger than that and not needed
#define EDITctrlLimitSize 65536
// check if we should reduce size of buffer
HLOCAL horgmem = (HLOCAL) SendMessage(EM_GETHANDLE,0,0);
SIZE_T sizeused=LocalSize(horgmem);
int cbCh = sizeof(TCHAR) > 1 ? sizeof(TCHAR) : IsUsingComCtlV6() ? sizeof(WCHAR) : sizeof(char);
if (sizeused > EDITctrlLimitSize && (string.GetLength()*cbCh) < EDITctrlLimitSize) {
// EM_GETHANDLE says to not change the data, yet EM_SETHANDLE tells you to
// get the handle then LocalFree it, so why not just LocalReAlloc it and set
// again.
HLOCAL hnewmem=(HLOCAL) LocalReAlloc(horgmem, EDITctrlLimitSize, LMEM_MOVEABLE);
if (hnewmem) {
// zero full buffer because seems like good thing to do
LPVOID pnewmem=LocalLock(hnewmem);
if (pnewmem) {
memset(pnewmem, 0, EDITctrlLimitSize);
LocalUnlock(hnewmem);
}
// viola - process memory reduced.
SendMessage(EM_SETHANDLE, (WPARAM) hnewmem, 0);
}
}