Memory detection

1100110 10equals2 at gmail.com
Thu Aug 23 20:03:38 PDT 2012


On Thu, 23 Aug 2012 18:37:59 -0500, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Charles Hixson:
>
>> Is it possible to detect when a program is using, say, 90% of the  
>> memory that is available, so that I can take steps to reduce usage?
>
> With computers that have virtual memory this is not so easy to do. I  
> think you have to use operating system-specific code to ask the OS about  
> the physical available memory, about the virtual memory used, the rate  
> of virtual memory swapping, and use those three values in some way.
>
> Bye,
> bearophile

On linux this is not so difficult to do.

Those values are generally in /proc, and it seems to be portable across  
pretty much every distro with a relatively recent kernel.

I have an extremely half-assed bit of code that prints the load average  
and the totaly % of mem used to my tmux session.
It gives the exact same values that are seen in top, or htop.(without the  
overhead of parsing their output, cause that takes ~500ms, way too slow.)
It would make a decent starting point at least.

I would imagine that you wouldn't even need to know the % of memory YOUR  
program is using, just the general percentage overall.

After all, no matter who is eating all the memory, shit's about to hit the  
fan if *someone* doesn't free some memory.
-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list