UProcessInfo.cpp
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 
22 #ifdef _WIN32
23 #include "Windows.h"
24 #include "Psapi.h"
25 #elif __APPLE__
26 #include <sys/resource.h>
27 #else
28 #include <fstream>
29 #include <stdlib.h>
30 #include "rtabmap/utilite/UStl.h"
31 #endif
32 
34 
36 
37 // return in bytes
39 {
40  long int memoryUsage = -1;
41 
42 #ifdef _WIN32
43  HANDLE hProc = GetCurrentProcess();
44  PROCESS_MEMORY_COUNTERS info;
45  BOOL okay = GetProcessMemoryInfo(hProc, &info, sizeof(info));
46  if(okay)
47  {
48  memoryUsage = info.WorkingSetSize;
49  }
50 #elif __APPLE__
51  rusage u;
52  if(getrusage(RUSAGE_SELF, &u) == 0)
53  {
54  memoryUsage = u.ru_maxrss;
55  }
56 #else
57  std::fstream file("/proc/self/status", std::fstream::in);
58  if(file.is_open())
59  {
60  std::string bytes;
61  while(std::getline(file, bytes))
62  {
63  if(bytes.find("VmRSS") != bytes.npos)
64  {
65  std::list<std::string> strs = uSplit(bytes, ' ');
66  if(strs.size()>1)
67  {
68  memoryUsage = atol(uValueAt(strs,1).c_str()) * 1024;
69  }
70  break;
71  }
72  }
73  file.close();
74  }
75 #endif
76 
77  return memoryUsage;
78 }
bytes
UProcessInfo.h
UProcessInfo::~UProcessInfo
virtual ~UProcessInfo()
Definition: UProcessInfo.cpp:35
UProcessInfo::UProcessInfo
UProcessInfo()
Definition: UProcessInfo.cpp:33
info
else if n * info
uSplit
std::list< std::string > uSplit(const std::string &str, char separator=' ')
Definition: UStl.h:564
c_str
const char * c_str(Args &&...args)
UStl.h
Wrappers of STL for convenient functions.
UProcessInfo::getMemoryUsage
static long int getMemoryUsage()
Definition: UProcessInfo.cpp:38
file
file
uValueAt
V & uValueAt(std::list< V > &list, const unsigned int &pos)
Definition: UStl.h:380


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:23