shaze
shaze
CC#
Created by shaze on 9/28/2024 in #help
Custom Memory Allocator
I need to manage millions of rather long lived (minutes to weeks) byte arrays that contain hundreds of KBs in memory. I need to be able to release the arrays if the memory pressure is too high and then reuse the RAM for arrays with other sizes. It is an in-memory database , which contains hot data from multiple TB of disk storage. Since a lot of IO is involved a lot of async functions are called and I can not make sure that the thread allocating is also the thread releasing memory. I think doing this with managed memory is a bad idea, because I can not manually free stuff or do anything against fragmentation. First I just used a pool based on NativeMemory.Alloc, but since it just calls malloc which is also not optimized for my usecase, this also leads to fragmentation over a couple of weeks of runtime. Now I built my own Memory allocator based on MemoryMappedFile and it seems to work really well. My questions are: Is this a bad idea, is there a better solution and what should I pay attention to when doing this?
3 replies