diff --git a/Modules/Core/include/mitkMemoryUtilities.h b/Modules/Core/include/mitkMemoryUtilities.h index 33e94a49bc..84fdc6c66f 100644 --- a/Modules/Core/include/mitkMemoryUtilities.h +++ b/Modules/Core/include/mitkMemoryUtilities.h @@ -1,91 +1,39 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ #ifndef _MITK_MEMORY_UTILITIES_H_ #define _MITK_MEMORY_UTILITIES_H_ #include #include namespace mitk { - class MITKCORE_EXPORT MemoryUtilities + namespace MemoryUtilities { - public: /** * Returns the memory usage of the current process in bytes. - * On linux, this refers to the virtual memory allocated by + * On Linux, this refers to the virtual memory allocated by * the process (the VIRT column in top). - * On windows, this refery to the size in bytes of the working + * On Windows, this refers to the size in bytes of the working * set pages (the "Speicherauslastung" column in the task manager). */ - static size_t GetProcessMemoryUsage(); + MITKCORE_EXPORT size_t GetProcessMemoryUsage(); /** * Returns the total size of physical memory in bytes */ - static size_t GetTotalSizeOfPhysicalRam(); - - /** - * Allocates an array of a given number of elements. Each element - * has a size of sizeof(ElementType). The function returns nullptr, if the array - * could not be allocated. - * @param numberOfElements the number of elements of the array - * @param noThrow if set to false, an exception is thrown if memory allocation - * fails. If set to true, a itk::MemoryAllocationError is thrown - * @returns a pointer to the allocated array. If noThrow == true, nullptr is returned - * if memory allocation failed. - */ - template - static ElementType *AllocateElements(size_t numberOfElements, bool noThrow = false) - { - // Encapsulate all image memory allocation here to throw an - // exception when memory allocation fails even when the compiler - // does not do this by default. - ElementType *data = nullptr; - try - { - data = new ElementType[numberOfElements]; - } - catch (...) - { - data = nullptr; - } - if ((data == nullptr) && (noThrow == false)) - { - throw itk::MemoryAllocationError(__FILE__, __LINE__, "Failed to allocate memory.", ITK_LOCATION); - } - return data; - } - - /** - * Deletes an array of elements previously allocated by AllocateElements. - * @param elements the array to delete. Not that nullptr is an accepted value. - */ - template - static void DeleteElements(ElementType *elements) - { - if (elements != nullptr) - { - delete[] elements; - } - } - - protected: -#ifndef _MSC_VER - static int ReadStatmFromProcFS( - int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages); -#endif - }; -} // end of namespace mitk + MITKCORE_EXPORT size_t GetTotalSizeOfPhysicalRam(); + } +} #endif diff --git a/Modules/Core/src/DataManagement/mitkMemoryUtilities.cpp b/Modules/Core/src/DataManagement/mitkMemoryUtilities.cpp index c089328d1f..6d510f7a83 100755 --- a/Modules/Core/src/DataManagement/mitkMemoryUtilities.cpp +++ b/Modules/Core/src/DataManagement/mitkMemoryUtilities.cpp @@ -1,115 +1,98 @@ /*============================================================================ The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center (DKFZ) All rights reserved. Use of this source code is governed by a 3-clause BSD license that can be found in the LICENSE file. ============================================================================*/ -#include "mitkMemoryUtilities.h" +#include -#include #if _MSC_VER #include #include #elif defined(__APPLE__) #include #include #include #include #else #include #include +#include #endif -/** - * Returns the memory usage of the current process in bytes. - * On linux, this refers to the virtual memory allocated by - * the process (the VIRT column in top). - * On windows, this refery to the size in bytes of the working - * set pages (the "Speicherauslastung" column in the task manager). - */ size_t mitk::MemoryUtilities::GetProcessMemoryUsage() { -#if _MSC_VER size_t size = 0; + +#if _MSC_VER DWORD pid = GetCurrentProcessId(); PROCESS_MEMORY_COUNTERS pmc; + HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); - if (hProcess == nullptr) - return 0; - if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) + + if (hProcess != nullptr) { - size = pmc.WorkingSetSize; + if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)) != 0) + size = pmc.WorkingSetSize; + + CloseHandle(hProcess); } - CloseHandle(hProcess); - return size; #elif defined(__APPLE__) struct task_basic_info t_info; mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; task_info(current_task(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count); - size_t size = t_info.virtual_size; - return size; + size = t_info.virtual_size; #else - int size, res, shared, text, sharedLibs, stack, dirtyPages; - if (!ReadStatmFromProcFS(&size, &res, &shared, &text, &sharedLibs, &stack, &dirtyPages)) - return (size_t)size * getpagesize(); - else - return 0; + std::ifstream statm("/proc/self/statm"); + + if (statm.is_open()) + { + size_t resident = 0; + size_t shared = 0; + + statm >> size >> resident >> shared; + + statm.close(); + + if (shared < resident) + size = resident - shared; // That's what the GNOME System Monitor reports as process memory + } #endif - return 0; + + return size; } -/** - * Returns the total size of physical memory in bytes - */ size_t mitk::MemoryUtilities::GetTotalSizeOfPhysicalRam() { #if _MSC_VER MEMORYSTATUSEX statex; statex.dwLength = sizeof(statex); - GlobalMemoryStatusEx(&statex); - return (size_t)statex.ullTotalPhys; + + if (GlobalMemoryStatusEx(&statex) != 0) + return statex.ullTotalPhys; #elif defined(__APPLE__) int mib[2]; - int64_t physical_memory; mib[0] = CTL_HW; mib[1] = HW_MEMSIZE; + + int64_t physical_memory; size_t length = sizeof(int64_t); + sysctl(mib, 2, &physical_memory, &length, nullptr, 0); + return physical_memory; #else struct sysinfo info; + if (!sysinfo(&info)) return info.totalram * info.mem_unit; - else - return 0; #endif -} -#ifndef _MSC_VER -#ifndef __APPLE__ -int mitk::MemoryUtilities::ReadStatmFromProcFS( - int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages) -{ - int ret = 0; - FILE *f; - f = fopen("/proc/self/statm", "r"); - if (f) - { - size_t ignored = fscanf(f, "%d %d %d %d %d %d %d", size, res, shared, text, sharedLibs, stack, dirtyPages); - ++ignored; - fclose(f); - } - else - { - ret = -1; - } - return ret; + return 0; } -#endif -#endif