mmap problem, allocates huge amounts of memory

前端 未结 8 787
野趣味
野趣味 2020-12-23 18:17

I got some huge files I need to parse, and people have been recommending mmap because this should avoid having to allocate the entire file in-memory.

But looking at

8条回答
  •  暖寄归人
    2020-12-23 18:47

    You can also use fadvise(2) (and madvise(2), see also posix_fadvise & posix_madvise ) to mark mmaped file (or its parts) as read-once.

    #include  
    
    int madvise(void *start, size_t length, int advice);
    

    The advice is indicated in the advice parameter which can be

    MADV_SEQUENTIAL 
    

    Expect page references in sequential order. (Hence, pages in the given range can be aggressively read ahead, and may be freed soon after they are accessed.)

    Portability: posix_madvise and posix_fadvise is part of ADVANCED REALTIME option of IEEE Std 1003.1, 2004. And constants will be POSIX_MADV_SEQUENTIAL and POSIX_FADV_SEQUENTIAL.

提交回复
热议问题