1

In my program, I use tcmalloc for memory allocation and release. In order to keep the memory back to the memory in time, after the completion of the call, using** MallocExtension::instance()->ReleaseFreeMemory()**. However, this only ensures that real memory is freed and virtual memory is still occupied.

My program needs to mount a file to a specified memory address through shmat after a series of operations. If tcmalloc uses too much virtual memory, the mount address may become unavailable.

Is there a way to free virtual memory in tcmalloc, or is there a function available in Linux?

int main() {
    int i = 0;
    while (i < 100) {
        char* a = (char*)malloc(1024 * 1024 * 2 * i); 
        malloc_stats();
        free(a);
        MallocExtension::instance()->ReleaseFreeMemory();
        malloc_stats();
        i++;
        usleep(1000 * 300);
    }   
    // in last loop , actual memory used  is small , but Virtual address space used is big...
    // if now I use shmat to mount something, it maybe fail.
    return 0;
}  
MALLOC:      207634928 (  198.0 MiB) Bytes in use by application
MALLOC: +            0 (    0.0 MiB) Bytes in page heap freelist
MALLOC: +        56408 (    0.1 MiB) Bytes in central cache freelist
MALLOC: +            0 (    0.0 MiB) Bytes in transfer cache freelist
MALLOC: +          440 (    0.0 MiB) Bytes in thread cache freelists
MALLOC: +     11251864 (   10.7 MiB) Bytes in malloc metadata
MALLOC:   ------------
MALLOC: =    218943640 (  208.8 MiB) Actual memory used (physical + swap)
MALLOC: +  10130219008 ( 9660.9 MiB) Bytes released to OS (aka unmapped)
MALLOC:   ------------
MALLOC: =  10349162648 ( 9869.7 MiB) **Virtual address space used**

I tried to use MallocExtension::instance()->ReleaseFreeMemory() , but it only frees actual memory, but for virtual address. I hope the virtual addresses are also released.

Yale
  • 13
  • 2

0 Answers0