日期:2014-05-16 浏览次数:20866 次
#include <sys/sysinfo.h> #include <unistd.h> #include <string.h> void show(struct sysinfo *_sys) { printf("Total usable main memory size %d\nAvailable memory size %d\n",_sys->totalram, _sys->freeram); printf("Amount of shared memory %d\nMemory used by buffers %d\n",_sys->sharedram,_sys->bufferram); printf("Total swap space size %d\nswap space still available %d\n", _sys->totalswap, _sys->freeswap); printf("Number of current processes %d\n", _sys->procs); printf("Memory unit size in bytes %ld\n", _sys->mem_unit); } main() { static char suc[]="------------- alloc successful ! --------------", fai[]="--------------- Fail to alloc ! ----------------"; struct sysinfo _sys; char *pt; sysinfo(&_sys); show(&_sys); pt=(char *)malloc(80000000); /* 此值可以增大,以强化测试效果 */ if(pt){ strcpy(pt, suc); puts(pt); } else puts(fai); sysinfo(&_sys); show(&_sys); }
------解决方案--------------------
Total usable main memory size 2117382144
Available memory size 90734592
Amount of shared memory 0
Memory used by buffers 95215616
Total swap space size 0
swap space still available 0
Number of current processes 86
Memory unit size in bytes 1
------------- alloc successful ! --------------
Total usable main memory size 2117382144
Available memory size 90734592
Amount of shared memory 0
Memory used by buffers 95215616
Total swap space size 0
swap space still available 0
Number of current processes 86
Memory unit size in bytes 1
//释放了~