앱 초기 실행 시에 동시에 스크립트를 로딩할 경우 파싱용 버퍼 때문에 동시에 메모리를 많이 사용하게 된다. 스크립트 파싱 후 메모리를 해제해도 os 로 바로 반환되지 않아 동시에 여러 앱을 실행하게 되면 os 메모리 부족이 발생할 수 있다. 초기화가 끝났다면 사용하지 않는 메모리를 바로 반환할 필요가 있다. 이 때 linux 에서는 malloc_trim 이라는 api 를 사용하면 된다.

https://man7.org/linux/man-pages/man3/malloc_trim.3.html

 

malloc_trim(3) - Linux manual page

malloc_trim(3) — Linux manual page MALLOC_TRIM(3) Linux Programmer's Manual MALLOC_TRIM(3) NAME         top malloc_trim - release free memory from the heap SYNOPSIS         top #include int malloc_trim(size_t pad); DESCRIPTION         top The

man7.org

Win32 에서는 HeapCompact 라는 api 를 사용했던 것 같다.

https://learn.microsoft.com/en-us/windows/win32/api/heapapi/nf-heapapi-heapcompact

 

HeapCompact function (heapapi.h) - Win32 apps

Returns the size of the largest committed free block in the specified heap. If the Disable heap coalesce on free global flag is set, this function also coalesces adjacent free blocks of memory in the heap.

learn.microsoft.com

 

728x90

+ Recent posts