How to workaround heap fragmentation in a C ++ server program?

fragmentation of the heap can cause the server application, which is expected to run continuously for many months, to suddenly start working incorrectly, thinking that it is from memory.

Suppose I did my best to minimize heap fragmentation at run time in my VC ++ server application, but still it creates and causes problems. I could, for example, automatically restart the application every month or every one and a half million requests processed - it is safe to stop it and it is safe to start again with a new heap. What else can I do to circumvent heap fragmentation?

+3
source share
4 answers

same answer as here: How to detect and evaluate heap fragmentation in my C ++ program?

write your own memory manager adapted to your memory allocation patterns. Or buy one (for example, a clever pile).

Since fragmentation depends on your memory allocation / deallocation patterns, the best answer is hard to give. But you can take a look at fixed-sized distributors or take a look at the smart heap page as they handle the distribution. There are also many works on this subject. Try for example www.memorymanagement.org

Or you can take a look at FastMM4 - it's open source, but in Pascal / Delphi

. : . , . , , . , , " " ".

+3

- .

  HANDLE heaps[1025];
  DWORD nheaps = GetProcessHeaps((sizeof(heaps) / sizeof(HANDLE)) - 1, heaps);
  for (DWORD i = 0; i < nheaps; ++i) {
    ULONG  enableLFH = 2;
    HeapSetInformation(heaps[i], HeapCompatibilityInformation, &enableLFH, sizeof(enableLFH));
  }

Vista/Server 2008... , , , .

​​ Windows 2000, Windows Vista.

vmmap, , .

+3

, , , , , - - , , , .

You can use VirtualQueryEx to traverse your heap and find the largest free contiguous area. For an example of how to do this, see In this article .

0
source

An obvious workaround is to dig up old solutions that were invented in the past. For example, opaque handles for moving objects instead of raw pointers. This allows you to defragment a bunch.

0
source

Source: https://habr.com/ru/post/1720540/


All Articles