根据/proc/meminfo对空闲内存进行占用


#include <stdio.h> #include <sys/sysinfo.h> #include <linux/kernel.h> /* 包含sysinfo结构体信息*/ #include <unistd.h> #include <string> #include <iostream> #include <fstream> #include <map> #include <vector> #include <assert.h> #include <stdlib.h> using namespace std; /////////////////////////////////////////////////// // Item Names which should be corresponded to the enum below restrictly const char * ItemCheckName[] = { "MemTotal", "MemFree", "Buffers", "Cached" }; enum ITEMCHECKNAME { MEMTOTAL = 0, MEMFREE, BUFFERS, CACHED }; const int INVALID_VALUE = -1; const char* MEM_INFO_FILE_NAME = "/proc/meminfo"; bool isDebugging = false; ////////////////////////////////////////////////////// string trim(const string& str) { string::size_type pos = str.find_first_not_of(' '); if (pos == string::npos) { return str; } string::size_type pos2 = str.find_last_not_of(' '); if (pos2 != string::npos) { return str.substr(pos, pos2 - pos + 1); } return str.substr(pos); } int split(const string& str, vector<string>& ret_, string sep = ",") { if (str.empty()) { return 0; } string tmp; string::size_type pos_begin = str.find_first_not_of(sep); string::size_type comma_pos = 0; while (pos_begin != string::npos) { comma_pos = str.find(sep, pos_begin); if (comma_pos != string::npos) { tmp = str.substr(pos_begin, comma_pos - pos_begin); pos_begin = comma_pos + sep.length(); } else { tmp = str.substr(pos_begin); pos_begin = comma_pos; } if (!tmp.empty()) { ret_.push_back(tmp); tmp.clear(); } } return 0; } bool CheckAllBeenSet(vector<pair<string, int> > itemsToCheck) { vector<pair<string, int> >::iterator it = itemsToCheck.begin(); while (it != itemsToCheck.end()) { if (it->second == INVALID_VALUE) { return false; } it++; } return true; } void PrintItems(vector<pair<string, int> > itemsToCheck) { vector<pair<string, int> >::iterator it = itemsToCheck.begin(); while (it != itemsToCheck.end()) { cout << "KEY = " << it->first << " , VALUE = " << it->second << " KB "<< endl; it++; } } unsigned int CheckFreeMemInKByte(vector<pair<string, int> > itemsToCheck) { // 空闲内存计算方式:如果Cached值大于MemTotal值则空闲内存为MemFree值,否则空闲内存为MemFree值+Buffers值+Cached值 int rlt; if (itemsToCheck[CACHED].second > itemsToCheck[MEMTOTAL].second) { rlt = itemsToCheck[MEMFREE].second; if (isDebugging) { cout << "CACHED(" << itemsToCheck[CACHED].second << "KB) > MEMTOTAL(" << itemsToCheck[MEMTOTAL].second << "KB)\n"; cout << "FreeMemInKb is " << rlt << "KB\n"; } } else { rlt = itemsToCheck[CACHED].second + itemsToCheck[MEMFREE].second + itemsToCheck[BUFFERS].second; if (isDebugging) { cout << "CACHED(" << itemsToCheck[CACHED].second << "KB) <= MEMTOTAL(" << itemsToCheck[MEMTOTAL].second << "KB)\n"; cout << "FreeMemInKb is " << rlt << "KB\n"; } } return rlt; } // usage int main(int argc, char *agrv[]) { if (argc < 3 || argc > 4) { cout << "Usage :\n memCons fromTotalMem freePercentage [isDebugging]\n"; cout << "For example : \'memCons 0 1\'\n means to take 99% of freeMem, that is to leave only 1% out of free memory\n"; cout << "For example : \'memCons 1 1\'\n means to take 99% of totalMem, that is to leave only 1% out of all the memory\n"; cout << "For example : \'memCons 1 1 1\'\n means in the debugging mode\n"; return -1; } bool fromTotalMem = atoi(agrv[1]) == 1 ? true : false; int freePercentage = atoi(agrv[2]); isDebugging = (argc == 4 && atoi(agrv[3]) == 1) ? true : false; if (!(freePercentage > 0 && freePercentage < 100)) { cout << "the second argument of memCons must between 0 and 100"; return -1; } struct sysinfo s_info; int error; error = sysinfo(&s_info); printf("the followings are output from \'sysinfo\' call \n\ncode error=%d\n",error); printf("Uptime = %ds\nLoad: 1 min%d / 5 min %d / 15 min %d\n" "RAM: total %d / free %d /shared%d\n" "Memory in buffers = %d\nSwap:total%d/free%d\n" "Number of processes = %d\n\n\n", s_info.uptime, s_info.loads[0], s_info.loads[1], s_info.loads[2], s_info.totalram, s_info.freeram, s_info.totalswap, s_info.freeswap, s_info.procs ); vector< pair<string, int> > itemsToCheck; std::pair <std::string, int> memTotal(ItemCheckName[MEMTOTAL], INVALID_VALUE); itemsToCheck.push_back(memTotal); std::pair <std::string, int> memfreePair(ItemCheckName[MEMFREE], INVALID_VALUE); itemsToCheck.push_back(memfreePair); std::pair <std::string, int> buffers(ItemCheckName[BUFFERS], INVALID_VALUE); itemsToCheck.push_back(buffers); std::pair <std::string, int> cached(ItemCheckName[CACHED], INVALID_VALUE); itemsToCheck.push_back(cached); vector<string> splitedWords; ifstream infile(MEM_INFO_FILE_NAME); if (infile.fail()) { cerr << "error in open the file"; return false; } int hitCnt = itemsToCheck.size(); while(hitCnt != 0) { splitedWords.clear(); char temp[100]; infile.getline(temp, 100); const string tmpString = temp; split(tmpString, splitedWords, ":"); // use the first part to check whether to continue splitedWords[0] = trim(splitedWords[0]); int foundIndex = -1; for (int i = 0; i < itemsToCheck.size(); i++) { if (itemsToCheck[i].first == splitedWords[0]) { foundIndex = i; hitCnt--; break; } } if (foundIndex == -1) { continue; } // check the number string numberInString = trim(splitedWords[1]); int firstNotNumberPos = numberInString.find_first_not_of("123456789"); numberInString.substr(0, firstNotNumberPos); int num = atoi(numberInString.c_str()); // insert into container itemsToCheck[foundIndex].second = num; if (infile.eof()) { break; } } infile.close(); PrintItems(itemsToCheck); if (CheckAllBeenSet(itemsToCheck) == false) { cout << "Error in checking " << MEM_INFO_FILE_NAME << endl; return -1; } // set used memory according to the requirements long long memToUse = 0; long long freeMemCount = 0; if (isDebugging) { cout << "Need memory use in total one ? " << fromTotalMem << endl; } if (!fromTotalMem) { if (isDebugging) { cout << "Need memory use in free one\n"; } freeMemCount = CheckFreeMemInKByte(itemsToCheck); } else { if (isDebugging) { cout << "Need memory use in total one\n"; cout << "total memory is " << itemsToCheck[MEMTOTAL].second << "KB, that " << itemsToCheck[MEMTOTAL].second * 1024 << "B" << endl; } freeMemCount = itemsToCheck[MEMTOTAL].second; } cout << "Free Mem Count is " << freeMemCount << "KB" << endl; memToUse = freeMemCount * ((double)1 - (double)((double)freePercentage / (double)100) ); cout << "MemToUse is " << memToUse << "KB" << endl; char* memConsumer[1024]; int j = 0; for (; j < 1024; j++) { memConsumer[j] = NULL; } try { for (j = 0; j < 1024; j++) { if (memConsumer[j] == NULL) { memConsumer[j] = new char[memToUse]; } for (int i = 0; i < memToUse; i++) { memConsumer[j][i] = '5'; } } } catch(std::bad_alloc) { // swallow the exception and continue cout << "no more memory can be allocated, already alloced " << j * memToUse << "B"; } while (1) { sleep(2); } return 0; }

 

posted @ 2013-07-22 16:14  aicro  阅读(1925)  评论(0编辑  收藏  举报