1

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

Thanks in advance!

rralf
  • 1,202
  • 15
  • 27
  • how about it malloc fails, you free up some memory? if you run out of memory while allocating a new stack frame, there isn't much you can do... – Grady Player May 20 '13 at 03:10
  • You can go to files approach as you mentioned can consider bitmap files also.Other than that you can go with the memory pool option where you can allocate memory one time and use memory from pool and give back memory to pool when not required. – Astro - Amit May 20 '13 at 03:10
  • @GradyPlayer: Then I would have to malloc all the time to recognize memory shortage. Besides of that my process could still get killed. – rralf May 20 '13 at 03:13
  • what kernel are you using? a memory warning signal is a feature of several kernels... – Grady Player May 20 '13 at 03:13
  • @GradyPlayer: Ok, didn't know about that feature. I'll search for it. – rralf May 20 '13 at 03:15
  • @GradyPlayer: I wasn't able to find anything out about "memory warning signals" or sth. like that. Do you have any references? – rralf May 20 '13 at 13:56

1 Answers1

0

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.

Laz
  • 6,036
  • 10
  • 41
  • 54
  • Hmm, but I don't want to touch my kernel (i'm using linux). I want to do everything in userspace. – rralf May 20 '13 at 14:53