How Freepascal memory manager works to increase arrays

Recently, I came across a problem in the framework of the FreePascal project that I am developing: an application requires a search array, which can become very large at runtime (several million entries). Each element of the array has a size of about 8 bytes.

I observed the following behavior of my application: if the array is already quite large (~ 130 MB), another increase will lead to a peak in memory consumption and, possibly, to a variable increase in the RAM used.

As far as I understand, the peak can be explained by the internal behavior of the SetLength () method, which allocates memory with the size of a new array and then copies the old array to a new destination in memory.

But when considering the sudden increase in used memory, it seemed that there were situations when the "old" memory was not freed, which led to double the use of RAM.

I was able to reproduce this behavior more clearly when I raised the steps in which the array was enlarged.

To get rid of this problem, I changed the memory manager to CMem, and the problem disappeared.

Unfortunately, I did not find a clear description of the Free Pascal memory manager, and I can only guess that the space of the "old" (small) array is not used, because the built-in memory manager wants the heap not to be fragmented all the time, but I could not prove it.

Do any of you have a source that describes the main functions of the Free Pascal memory manager and C memory manager and / or the differences between them?

Thank you very much good wishes

Alex

+4

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


All Articles