Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created December 3, 2011 13:30
Show Gist options
  • Save dmikurube/1427127 to your computer and use it in GitHub Desktop.
Save dmikurube/1427127 to your computer and use it in GitHub Desktop.
Memory allocation test code
// g++ -L<path-to-libtcmalloc.so> -ltcmalloc memory-test.cc
// env LD_LIBRARY_PATH=<path-to-libtcmalloc.so>b HEAPPROFILE=<prefix-to-output-files HEAP_PROFILE_INUSE_INTERVAL=1048576 ./a.out
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
class Sub {
public:
Sub(int x) {
sprintf(s_, "%d", x);
}
char s_[128];
};
class Klass {
public:
Klass() __attribute__((noinline)) : x_(0), y_(0), z_(0) {
}
Klass(int x) __attribute__((noinline));
Klass(int x, int y) __attribute__((noinline));
Klass(int x, int y, int z) __attribute__((noinline));
inline void alloc(int x) /*__attribute__((noinline))*/ {
sub_ = new Sub(x);
}
int x_;
int y_;
int z_;
char s_[1024];
Sub* sub_;
};
Klass::Klass(int x) : x_(x), y_(x), z_(x) {
}
Klass::Klass(int x, int y) {
x_ = x;
y_ = y;
z_ = y;
sub_ = new Sub(x);
}
Klass::Klass(int x, int y, int z) {
asm("");
x_ = x;
asm("");
y_ = y;
asm("");
z_ = z;
asm("");
}
const int A_NUM = 10;
const int B_NUM = 20;
const int C_NUM = 30;
const int D_NUM = 40;
const int SUB_NUM = 15;
int main() {
Klass* a[A_NUM];
Klass* b[B_NUM];
Klass* c[C_NUM];
Klass* d[D_NUM];
void* dummy_pointer;
for (int i = 0; i < A_NUM; ++i)
a[i] = new Klass();
for (int i = 0; i < B_NUM; ++i)
b[i] = new Klass(103);
for (int i = 0; i < C_NUM; ++i)
c[i] = new Klass(14, 10);
for (int i = 0; i < D_NUM; ++i)
d[i] = new Klass(8, 21, 4);
for (int i = 0; i < SUB_NUM; ++i)
b[i]->alloc(i * 3);
dummy_pointer = (void *)malloc(1024 * 1024 * 1);
for (int i = 0; i < D_NUM; ++i)
delete d[i];
for (int i = 0; i < C_NUM; ++i)
delete c[i];
for (int i = 0; i < B_NUM; ++i)
delete b[i];
for (int i = 0; i < A_NUM; ++i)
delete a[i];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment