Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMemoryUtilities.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "mitkMemoryUtilities.h"
18 
19 #include <stdio.h>
20 #if _MSC_VER || __MINGW32__
21 #include <windows.h>
22 #include <psapi.h>
23 #elif defined(__APPLE__)
24 #include <mach/mach_host.h>
25 #include <mach/mach_init.h>
26 #include <mach/task.h>
27 #include <sys/sysctl.h>
28 #else
29 #include <sys/sysinfo.h>
30 #include <unistd.h>
31 #endif
32 
41 {
42 #if _MSC_VER || __MINGW32__
43  size_t size = 0;
44  DWORD pid = GetCurrentProcessId();
45  PROCESS_MEMORY_COUNTERS pmc;
46  HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
47  if (hProcess == NULL)
48  return 0;
49  if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))
50  {
51  size = pmc.WorkingSetSize;
52  }
53  CloseHandle(hProcess);
54  return size;
55 #elif defined(__APPLE__)
56  struct task_basic_info t_info;
57  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
58  task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
59  size_t size = t_info.virtual_size;
60  return size;
61 #else
62  int size, res, shared, text, sharedLibs, stack, dirtyPages;
63  if (!ReadStatmFromProcFS(&size, &res, &shared, &text, &sharedLibs, &stack, &dirtyPages))
64  return (size_t)size * getpagesize();
65  else
66  return 0;
67 #endif
68  return 0;
69 }
70 
75 {
76 #if _MSC_VER || __MINGW32__
77  MEMORYSTATUSEX statex;
78  statex.dwLength = sizeof(statex);
79  GlobalMemoryStatusEx(&statex);
80  return (size_t)statex.ullTotalPhys;
81 #elif defined(__APPLE__)
82  int mib[2];
83  int64_t physical_memory;
84  mib[0] = CTL_HW;
85  mib[1] = HW_MEMSIZE;
86  size_t length = sizeof(int64_t);
87  sysctl(mib, 2, &physical_memory, &length, NULL, 0);
88  return physical_memory;
89 #else
90  struct sysinfo info;
91  if (!sysinfo(&info))
92  return info.totalram * info.mem_unit;
93  else
94  return 0;
95 #endif
96 }
97 
98 #ifndef _MSC_VER
99 #ifndef __APPLE__
101  int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages)
102 {
103  int ret = 0;
104  FILE *f;
105  f = fopen("/proc/self/statm", "r");
106  if (f)
107  {
108  size_t ignored = fscanf(f, "%d %d %d %d %d %d %d", size, res, shared, text, sharedLibs, stack, dirtyPages);
109  ++ignored;
110  fclose(f);
111  }
112  else
113  {
114  ret = -1;
115  }
116  return ret;
117 }
118 #endif
119 #endif
static vcl_size_t GetTotalSizeOfPhysicalRam()
static int ReadStatmFromProcFS(int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages)
static vcl_size_t GetProcessMemoryUsage()