简体   繁体   English

动态内存分配/利用未使用的内存

[英]Dynamical Memory Allocation / Making use of unused memory

I'm going to write an application that needs a lot of memory dynamically. 我将编写一个动态需要大量内存的应用程序。 Most of the memory is used for caching purposes and is just used for speed ups. 大部分内存用于缓存,仅用于加速。 Those parts could actually be freed on demand. 这些部分实际上可以按需释放。

Unfortunately my kernel will kill the process if it runs out of memory. 不幸的是,如果内核用尽了内存,它将终止该进程。 But it could simply free memory. 但这可能只是释放内存。 So what I want is very similar to the linux page cache as it is explained here . 所以,我想是非常相似的,因为它解释了Linux页面缓存这里 Is it possible to implement such behaviour in userspace in a convenient way? 是否可以通过方便的方式在用户空间中实现这种行为?

I'm thinking about implementing such a cache with "cache files" which are stored on a ramfs/tmpfs with memory mapped file IO, but i'm sure, that there is a more comfortable way. 我正在考虑使用“高速缓存文件”实现这样的高速缓存,这些文件存储在具有内存映射文件IO的ramfs / tmpfs中,但是我敢肯定,有一种更舒适的方法。

Thanks in advance! 提前致谢!

Yes this should be possible. 是的,这应该是可能的。 Most kernels have a memory alloc method where the process sleeps until it gets the requested memory. 大多数内核都有一个内存分配方法,在该方法中,进程一直休眠直到获取请求的内存。 ( all the kernels ive worked with have). (所有与我合作的内核都有)。 If yours doesnt this may be a good time to implement one. 如果您不这样做,这可能是实施一个的好时机。 You could check out the kmem functions in linux. 您可以在linux中检查kmem函数。 However this is a passive way of doing what youve asked. 但是,这是按照您的要求做事的被动方式。 The process will be waiting until someone else frees up memory. 该过程将一直等到其他人释放内存。 If you want to free up memory from your own process address space when theres no memory, this can be done easily from user space. 如果要在没有内存的情况下从自己的进程地址空间中释放内存,则可以从用户空间轻松完成此操作。 You need to keep a journal of allocated memory and free the ones you dont need on demand when an alloc fails. 您需要保留已分配内存的日志,并在分配失败时按需释放不需要的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM