Go to the documentation of this file.00001 #include "CPMemUtils.h"
00002
00003 unsigned long getPhysicalMemorySize()
00004 {
00005 #ifdef __APPLE__
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 return 1024 * 1024 * 1024;
00027
00028 #else
00029
00030 #ifdef _MSC_VER
00031 MEMORYSTATUSEX statex;
00032
00033 statex.dwLength = sizeof (statex);
00034
00035 GlobalMemoryStatusEx (&statex);
00036
00037 return statex.ullTotalPhys;
00038
00039
00040 #else
00041
00042
00043
00044
00045
00046 unsigned long int pageSize = sysconf (_SC_PAGESIZE);
00047 unsigned long int pageNum = sysconf (_SC_PHYS_PAGES);
00048
00049
00050 #ifdef __CYGWIN__
00051 pageSize = 4096;
00052
00053 #endif
00054
00055
00056
00057 return pageSize * pageNum;
00058
00059 #endif
00060 #endif
00061
00062
00063 }
00064
00065 unsigned long getCurrentProcessMemoryUsage()
00066 {
00067 #ifdef __APPLE__
00068
00069
00070 struct task_basic_info t_info;
00071 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
00072 task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
00073 size_t size = t_info.resident_size;
00074 return size;
00075
00076 #else
00077 #ifdef _MSC_VER
00078
00079 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
00080
00081 if(hProcess)
00082 {
00083 PROCESS_MEMORY_COUNTERS ProcessMemoryCounters;
00084
00085 memset(&ProcessMemoryCounters, 0, sizeof(ProcessMemoryCounters));
00086
00087
00088 ProcessMemoryCounters.cb = sizeof(ProcessMemoryCounters);
00089
00090
00091 if(GetProcessMemoryInfo(hProcess, &ProcessMemoryCounters, sizeof(ProcessMemoryCounters)) == TRUE)
00092 {
00093 return ProcessMemoryCounters.WorkingSetSize;
00094 }
00095 else
00096 {
00097 return 0;
00098
00099 }
00100
00101
00102 CloseHandle(hProcess);
00103 }
00104 else
00105 return 0;
00106
00107 #else
00108 struct mallinfo info;
00109
00110
00111 info = mallinfo();
00112
00113 return info.arena;
00114
00115 #endif
00116 #endif
00117
00118
00119
00120 }
00121
00122 unsigned long getPlatformMemoryLimit()
00123 {
00124 #ifdef __APPLE__
00125
00126 #else
00127 #ifdef _MSC_VER
00128
00129
00130 #else
00131
00132 #endif
00133 #endif
00134
00135 static unsigned long memLimit = -1;
00136 if(memLimit == -1)
00137 {
00138 memLimit = (unsigned long)(getPhysicalMemorySize() * 0.75);
00139 }
00140 return memLimit;
00141 }
00142
00143
00144
00145
00146