当前位置: 技术问答>linux和unix
【求助】如何在应用程序中得到系统的剩余物理内存大小?
来源: 互联网 发布时间:2015-11-03
本文导语: RT,如何在应用程序中得到系统的剩余可用物理内存大小? | /* * get the mm usage */ int getOnceMemUsage() { float memusage; FILE *fp; char buffer[1024]; size_t byte_read; char *pos; int totalmem; int freemem;...
RT,如何在应用程序中得到系统的剩余可用物理内存大小?
|
/*
* get the mm usage
*/
int getOnceMemUsage()
{
float memusage;
FILE *fp;
char buffer[1024];
size_t byte_read;
char *pos;
int totalmem;
int freemem;
fp = fopen("/proc/meminfo", "r");
byte_read = fread(buffer, 1, sizeof(buffer), fp);
fclose(fp);
if (byte_read==0 || byte_read==sizeof(buffer))
return -1;
buffer[byte_read] = '';
pos = strstr(buffer, "MemTotal:");
if (NULL==pos)
return -1;
sscanf(pos, "MemTotal: %d kB", &totalmem);
pos = strstr(pos, "MemFree:");
if (NULL==pos)
return -1;
sscanf(pos, "MemFree: %d kB", &freemem);
memusage = (totalmem-freemem) * 100.0 / totalmem;
printf("### MemUsage(total:%d free:%d %MEM:%3.2f%)n", totalmem, freemem, memusage);
return 0;
}
* get the mm usage
*/
int getOnceMemUsage()
{
float memusage;
FILE *fp;
char buffer[1024];
size_t byte_read;
char *pos;
int totalmem;
int freemem;
fp = fopen("/proc/meminfo", "r");
byte_read = fread(buffer, 1, sizeof(buffer), fp);
fclose(fp);
if (byte_read==0 || byte_read==sizeof(buffer))
return -1;
buffer[byte_read] = '';
pos = strstr(buffer, "MemTotal:");
if (NULL==pos)
return -1;
sscanf(pos, "MemTotal: %d kB", &totalmem);
pos = strstr(pos, "MemFree:");
if (NULL==pos)
return -1;
sscanf(pos, "MemFree: %d kB", &freemem);
memusage = (totalmem-freemem) * 100.0 / totalmem;
printf("### MemUsage(total:%d free:%d %MEM:%3.2f%)n", totalmem, freemem, memusage);
return 0;
}
|
读取/proc/meminfo文件
|
TO
YanDong_8212(谢科)
你的办法不行
还有虚模内存
YanDong_8212(谢科)
你的办法不行
还有虚模内存
|
可以计算,但方法笨拙,直接得到还不太清楚。
不断的malloc能计算出来。
不断的malloc能计算出来。