Static Public Member Functions | Static Protected Member Functions

mitk::MemoryUtilities Class Reference

#include <mitkMemoryUtilities.h>

List of all members.

Static Public Member Functions

static vcl_size_t GetProcessMemoryUsage ()
static vcl_size_t GetTotalSizeOfPhysicalRam ()
template<typename ElementType >
static ElementType * AllocateElements (vcl_size_t numberOfElements, bool noThrow=false)
template<typename ElementType >
static void DeleteElements (ElementType *elements)

Static Protected Member Functions

static int ReadStatmFromProcFS (int *size, int *res, int *shared, int *text, int *sharedLibs, int *stack, int *dirtyPages)

Detailed Description

Definition at line 27 of file mitkMemoryUtilities.h.


Member Function Documentation

template<typename ElementType >
static ElementType* mitk::MemoryUtilities::AllocateElements ( vcl_size_t  numberOfElements,
bool  noThrow = false 
) [inline, static]

Allocates an array of a given number of elements. Each element has a size of sizeof(ElementType). The function returns NULL, if the array could not be allocated.

Parameters:
numberOfElementsthe number of elements of the array
noThrowif 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, NULL is returned if memory allocation failed.

Definition at line 55 of file mitkMemoryUtilities.h.

  {
    // 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 = NULL;
    try
    {
      data = new ElementType[numberOfElements];
    }
    catch(...)
    {
      data = NULL;
    }
    if( ( data == NULL ) && ( noThrow == false ) )
    {
      throw itk::MemoryAllocationError(__FILE__, __LINE__, "Failed to allocate memory.", ITK_LOCATION);
    }
    return data;
  }
template<typename ElementType >
static void mitk::MemoryUtilities::DeleteElements ( ElementType *  elements ) [inline, static]

Deletes an array of elements previously allocated by AllocateElements.

Parameters:
elementsthe array to delete. Not that NULL is an accepted value.

Definition at line 81 of file mitkMemoryUtilities.h.

  {
    if ( elements != NULL )
    {
      delete[] elements;  
    }  
  }
vcl_size_t mitk::MemoryUtilities::GetProcessMemoryUsage (  ) [static]

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).

Definition at line 41 of file mitkMemoryUtilities.cpp.

References FALSE, HANDLE(), and ReadStatmFromProcFS().

Referenced by QmitkDiffusionDicomImport::PrintMemoryUsage(), and QmitkMemoryUsageIndicatorView::UpdateMemoryUsage().

{
#if _MSC_VER || __MINGW32__
  size_t size = 0;
  DWORD pid = GetCurrentProcessId();
  PROCESS_MEMORY_COUNTERS pmc;
  HANDLE hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid );
  if ( hProcess == NULL )
      return 0;
  if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
  {
    size = pmc.WorkingSetSize;
  }
  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;
#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;
#endif 
  return 0;
}
vcl_size_t mitk::MemoryUtilities::GetTotalSizeOfPhysicalRam (  ) [static]

Returns the total size of phyiscal memory in bytes

Definition at line 76 of file mitkMemoryUtilities.cpp.

Referenced by QmitkDiffusionDicomImport::PrintMemoryUsage(), and QmitkMemoryUsageIndicatorView::UpdateMemoryUsage().

{
#if _MSC_VER || __MINGW32__
  MEMORYSTATUSEX statex;
  statex.dwLength = sizeof (statex);
  GlobalMemoryStatusEx (&statex);
  return (size_t) statex.ullTotalPhys;
#elif defined(__APPLE__)
  kern_return_t kr;
  host_basic_info_data_t hostinfo;
  int count = HOST_BASIC_INFO_COUNT;
  kr = host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostinfo, (mach_msg_type_number_t*)&count);
  if(kr == KERN_SUCCESS)
    return (size_t)hostinfo.memory_size;
  else
    return 0;
#else
  struct sysinfo info;
  if ( ! sysinfo( &info ) )
    return info.totalram * info.mem_unit;
  else
    return 0;
#endif 
}
int mitk::MemoryUtilities::ReadStatmFromProcFS ( int *  size,
int *  res,
int *  shared,
int *  text,
int *  sharedLibs,
int *  stack,
int *  dirtyPages 
) [static, protected]

Definition at line 103 of file mitkMemoryUtilities.cpp.

Referenced by GetProcessMemoryUsage().

{
  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;
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines