Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkMemoryUtilities.h"
14 
15 #include <cstdio>
16 #if _MSC_VER
17 #include <windows.h>
18 #include <psapi.h>
19 #elif defined(__APPLE__)
20 #include <mach/mach_host.h>
21 #include <mach/mach_init.h>
22 #include <mach/task.h>
23 #include <sys/sysctl.h>
24 #else
25 #include <sys/sysinfo.h>
26 #include <unistd.h>
27 #endif
28 
37 {
38 #if _MSC_VER
39  size_t size = 0;
40  DWORD pid = GetCurrentProcessId();
41  PROCESS_MEMORY_COUNTERS pmc;
42  HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
43  if (hProcess == nullptr)
44  return 0;
45  if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))
46  {
47  size = pmc.WorkingSetSize;
48  }
49  CloseHandle(hProcess);
50  return size;
51 #elif defined(__APPLE__)
52  struct task_basic_info t_info;
53  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
54  task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
55  size_t size = t_info.virtual_size;
56  return size;
57 #else
58  int size, res, shared, text, sharedLibs, stack, dirtyPages;
59  if (!ReadStatmFromProcFS(&size, &res, &shared, &text, &sharedLibs, &stack, &dirtyPages))
60  return (size_t)size * getpagesize();
61  else
62  return 0;
63 #endif
64  return 0;
65 }
66 
71 {
72 #if _MSC_VER
73  MEMORYSTATUSEX statex;
74  statex.dwLength = sizeof(statex);
75  GlobalMemoryStatusEx(&statex);
76  return (size_t)statex.ullTotalPhys;
77 #elif defined(__APPLE__)
78  int mib[2];
79  int64_t physical_memory;
80  mib[0] = CTL_HW;
81  mib[1] = HW_MEMSIZE;
82  size_t length = sizeof(int64_t);
83  sysctl(mib, 2, &physical_memory, &length, nullptr, 0);
84  return physical_memory;
85 #else
86  struct sysinfo info;
87  if (!sysinfo(&info))
88  return info.totalram * info.mem_unit;
89  else
90  return 0;
91 #endif
92 }
93 
94 #ifndef _MSC_VER
95 #ifndef __APPLE__
97  int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages)
98 {
99  int ret = 0;
100  FILE *f;
101  f = fopen("/proc/self/statm", "r");
102  if (f)
103  {
104  size_t ignored = fscanf(f, "%d %d %d %d %d %d %d", size, res, shared, text, sharedLibs, stack, dirtyPages);
105  ++ignored;
106  fclose(f);
107  }
108  else
109  {
110  ret = -1;
111  }
112  return ret;
113 }
114 #endif
115 #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()