test_memusage.cxx
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <sstream>
5 #include <vector>
6 //#include <unistd.h>
7 
8 using namespace std;
9 using namespace WireCell;
10 
11 int* suck(MemUsage& mu, long siz)
12 {
13  int *buf = new int[siz * 1024];
14  //sleep(1);
15  {
16  stringstream ss;
17  ss << "newed " << siz << "K";
18  cout << ss.str() << " " << mu(ss.str()) << endl;
19  }
20 
21  for (int ind=0; ind<siz; ++ind) {
22  buf[ind] = 0;
23  }
24  {
25  stringstream ss;
26  ss << "accessed " << siz << "K";
27  cout << ss.str() << " " << mu(ss.str()) << endl;
28  }
29 
30  return buf;
31 }
32 
33 void blow(MemUsage& mu, int siz, int* buf)
34 {
35  delete [] buf;
36  //sleep(1);
37  stringstream ss;
38  ss << "freed " << siz << "K";
39  cout << ss.str() << " " << mu(ss.str()) << endl;
40 }
41 
42 int main()
43 {
44  cout << "current size " << memusage_size() << endl;
45  cout << "current resident " << memusage_resident() << endl;
46  cout << "current shared " << memusage_shared() << endl;
47 
48  MemUsage mu;
49 
50  long sizes[] = {10, 100, 1000, 10000, 100000, 1000000, -1};
51  vector<int*> bufs;
52 
53  int ind = 0;
54  for (; sizes[ind] > 0; ++ind) {
55  int* buf = suck(mu, sizes[ind]);
56  bufs.push_back(buf);
57  }
58 
59  for (--ind; ind >= 0; --ind) {
60  blow(mu, sizes[ind], bufs[ind]);
61  }
62 
63  cout << "Summary:" << endl;
64  cout << mu.summary() << endl;
65 
66 }
double memusage_size()
Definition: MemUsage.cxx:57
STL namespace.
void blow(MemUsage &mu, int siz, int *buf)
double memusage_resident()
Definition: MemUsage.cxx:43
Definition: Main.h:22
std::string summary() const
Return summary up to now.
Definition: MemUsage.cxx:101
int * suck(MemUsage &mu, long siz)
int main()
QTextStream & endl(QTextStream &s)
double memusage_shared()
Definition: MemUsage.cxx:50