问题描述
我需要一种使用C或C ++的方法来从/dev/shm
获得可用内存.请注意,不幸的是,在我的Linux上的 ARM
体系结构中, ipcs
报告了错误的最大值.可用的内存信息,但是 df -h
正确地提供了 tmpfs
的当前可用内存.
I need a way in C or C++ to get the free memory available from /dev/shm
. Note that on my ARM
architecure on Linux, unfortunately, ipcs
reports a wrong max. available memory information, but df -h
correctly gives me the current available memory from tmpfs
.
问题是我试图通过 boost :: interprocess :: shared_memory_object :: truncate
分配共享内存,但是当内存不可用时此函数不会抛出.这个问题显然不是在 boost :: interprocess
中,而是来自底层的 ftruncate()
,当没有可用的内存时,该问题不会返回适当的错误( https://svn.boost.org/trac/boost/ticket/4374 ),所以 boost
不能抛出任何东西.
The problem is that I am trying to allocate shared memory via boost::interprocess::shared_memory_object::truncate
, but this function does not throw when the memory is not available. This problem is not apparently in boost::interprocess
, but comes from the underlying ftruncate()
which does not return the appropriate error when there is no memory available ( https://svn.boost.org/trac/boost/ticket/4374 ), so boost
cannot throw anything.
推荐答案
尝试使用statvfs glibc函数或statfs系统调用
Try the statvfs glibc function, or the statfs system call
#include <sys/statvfs.h>
int statvfs(const char *path, struct statvfs *buf);
#include <sys/vfs.h> /* or <sys/statfs.h> */
int statfs(const char *path, struct statfs *buf);
// in both structures you can get the free memory
// by the following formula.
free_Bytes = s->f_bsize * s->f_bfree
这篇关于如何从/dev/shm获取有关可用内存的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!