动态内存分配/利用未使用的内存 [英] Dynamical Memory Allocation / Making use of unused memory

查看:257
本文介绍了动态内存分配/利用未使用的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要编写动态需要大量内存的应用程序。
大多数内存被用于缓存目的,只是用于速度提升。
这些部分实际上可以根据需求释放。

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.

不幸的是,如果内存用完我的内核将终止进程。但它可以
简单的可用内存。所以,我要的是非常相似的Linux的页面缓存,因为它是
rel=\"nofollow\">。是否有可能实现一种方便的方式在用户空间这样的行为?

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. Is it possible to implement such behaviour in userspace in a convenient way?

我在想用缓存文件,这是存储在RAMFS /内存映射文件IO的tmpfs实施这样一个缓存,但我敢肯定,有一个更舒适的方式。

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.

在此先感谢!

推荐答案

是的,这应该是可能的。大多数内核有内存的alloc方法,其中的进程将休眠,直到它得到请求的内存。 (所有的内核与香港专业教育学院工作过)。如果您的犯规,这可能是实现一个的好时机。你可以检查出的kmem功能的Linux。
然而,这是在做什么您问的一个被动的方式。这个过程将一直等到别人释放内存。
如果您想从自己的进程地址空间中释放内存时,世界上没有其他的内存,这可以很容易地从用户空间完成的。你需要保持分配内存的日记并释放你不需要按需当页头失败的人。

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. 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.

这篇关于动态内存分配/利用未使用的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆