简体   繁体   English

将文本设置为大量数据后释放编辑控件堆?

[英]Free Heap of Edit Control after setting the text to a large amount of data?

I was looking at my process in Task manager after pulling in a large amount of data that went to a CEditView then setting back to small amount.在将大量数据拉入CEditView然后设置回少量数据后,我正在任务管理器中查看我的进程。 I noticed the commit size stayed large.我注意到提交大小仍然很大。 Then I used VMMMap and see it as well, so I did "Memory Usage" in VS2017 Diagnostic Tools.然后我用 VMMMap 也看到了,所以我在 VS2017 诊断工具中做了“内存使用”。 I see it's coming from ultimately the ::SetWindowText() call.我看到它最终来自::SetWindowText()调用。 So that obviously allocates a large buffer on the heap, but then when I set it back to a small amount, that allocate stays large.所以这显然在堆上分配了一个大缓冲区,但是当我将它设置回一个小量时,分配仍然很大。 The question is, is there a way I can have the Edit Control free up the memory it doesn't need for smaller amounts of text to reduce that committed memory?问题是,有没有办法让编辑控件释放 memory 它不需要更少量的文本来减少提交的 memory? Say, I can free it up before setting the new text and it would allocate as needed?说,我可以在设置新文本之前释放它,它会根据需要分配吗?

Thanks!!谢谢!!

  // From within CEditView
  
  // based on an answer RbMm using EM_GETHANDLE/EM_SETHANDLE and further
  // from https://stackoverflow.com/questions/5500237/how-to-use-em-sethandle-on-edit-control
 
  // 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);
    }
  }
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM