Chi-Tech
chi_console_memory.cc
Go to the documentation of this file.
3
4#if defined(__MACH__)
5#include <mach/mach.h>
6#endif
7#include <iostream>
8#include <fstream>
9#include <unistd.h>
10
11//###################################################################
12/**Gets the current memory usage.*/
14{
15 double mem = 0.0;
16#if defined(__MACH__)
17 struct mach_task_basic_info info;
18 mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
19 long long int bytes;
20 if(task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info,
21 &count) != KERN_SUCCESS)
22 {
23 bytes = 0;
24 }
25 bytes = info.resident_size;
26 mem = (double)bytes;
27#else
28 long long int llmem = 0;
29 long long int rss = 0;
30
31 std::string ignore;
32 std::ifstream ifs("/proc/self/stat", std::ios_base::in);
33 ifs >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore
34 >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore
35 >> ignore >> ignore >> llmem >> rss;
36
37 long long int page_size_bytes = sysconf(_SC_PAGE_SIZE);
38 mem = rss*page_size_bytes;
39 /*
40 FILE* fp = NULL;
41 if((fp = fopen( "/proc/self/statm", "r" )) == NULL)
42 return 0;
43 if(fscanf(fp, "%*s%*s%*s%*s%*s%lld", &llmem) != 1)
44 {
45 fclose(fp);
46 return 0;
47 }
48 fclose(fp);*/
49
50 //mem = llmem * (long long int)sysconf(_SC_PAGESIZE);
51#endif
52
53 CSTMemory mem_struct(mem);
54
55 return mem_struct;
56}
57
58//###################################################################
59/**Gets the current memory usage in megabytes.*/
61{
62 CSTMemory mem_struct = GetMemoryUsage();
63
64 return mem_struct.memory_mbytes;
65}
static double GetMemoryUsageInMB()
Get current memory usage in Megabytes.
static CSTMemory GetMemoryUsage()
Get current memory usage.