Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping memory usage within available amount

I'm writing a program (a theorem prover as it happens) whose memory requirement is "as much as possible, please"; that is, it can always do better by using more memory, for practical purposes without upper bound, so what it actually needs to do is use just as much memory as is available, no more and no less. I can figure out how to prioritize data to delete the lowest value stuff when memory runs short; the problem I'm trying to solve is how to tell when this is happening.

Ideally I would like a system call that returns "how much memory is left" or "are we out of memory yet?"; as far as I can tell, no such thing exists?

Of course, malloc can signal out of memory by returning 0 and new can call a handler; these aren't ideal signals, but would be better than nothing. A problem, however, is that I really want to know when physical memory is running out, so I can avoid going deep into swap and thereby making everything grind to a halt; I don't suppose there's any way to ask "are we having to swap yet?" or tell the operating system "don't swap on my account, just fail my requests if it comes to that"?

Another approach would be to find out how much RAM is in the machine, and monitor how much memory the program is using at the moment. As far as I know, there is generally no way to tell the former? I also get the impression there is no reliable way to tell the latter except by wrapping malloc/free with a bookkeeper function (which is then more problematic in C++).

Are there any approaches I'm missing?

The ideal would be a portable solution, but I suspect that's not going to happen. Failing that, a solution that works on Windows and another one that works on Unix would be nice. Failing that, I could get by with a solution that works on Windows and another one that works on Linux.

like image 742
rwallace Avatar asked Sep 24 '10 09:09

rwallace


People also ask

How do I keep track of memory usage?

To open up Resource Monitor, press Windows Key + R and type resmon into the search box. Resource Monitor will tell you exactly how much RAM is being used, what is using it, and allow you to sort the list of apps using it by several different categories. However, it doesn't offer much else.

What is the difference between available and free memory?

The difference between free memory vs. available memory in Linux is, that free memory is not in use and sits there doing nothing. While available memory is used memory that includes but is not limited to caches and buffers, that can be freed without the performance penalty of using swap space.

What percentage memory usage is normal?

Why 15-30% RAM Usage Is Normal? The RAM usage between 15-30% at idle might sound a bit high to you. However, Windows always has a reserved memory of around 0.8-2.4GB in anticipation of its use. This amount is usually dependent on your computer's hardware and its quality.


1 Answers

I think the most useful and flexible way to use all the memory available is to let the user specify how much memory to use.

Let the user write it in a config file or through an interface, then create an allocator (or something similar) that will not provide more than this memory.

That way, you don't have to find statistics about the current computer as this will allways be biased by the fact that the OS could also run other programs as well. Don't even talk about the way the OS will manage cache, the differences between 32 and 64 bit making adress space limit your allocations etc.

In the end, human intelligence (assuming the user know about the context of use) is cheaper to implement when provided by the user.

like image 174
Klaim Avatar answered Sep 29 '22 18:09

Klaim