Created
September 18, 2019 16:02
-
-
Save mshabunin/8f6d0d4d1ad26b8fdec878ab650a0df2 to your computer and use it in GitHub Desktop.
Memory fragmentation with posix_memalign
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <thread> | |
#include <utility> | |
#include <vector> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <iostream> | |
#include <malloc.h> | |
using namespace std; | |
const size_t sz = 3 * 1000 * 1000; | |
inline void delay() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } | |
// switch between raw malloc calls and OpenCV | |
#if 1 | |
// swtich between posix_memalign calls and malloc | |
#if 1 | |
#define INIT(a) 0 | |
#define POSIX_MEMALIGN(a, b, c) posix_memalign((a), (b), (c)) | |
#else | |
#define INIT(a) (char*)malloc((a)) | |
#define POSIX_MEMALIGN(a, b, c) | |
#endif | |
int main() | |
{ | |
char * buf = INIT(sz); | |
POSIX_MEMALIGN((void**)&buf, 64, sz); | |
memset(buf, 0, sz); | |
std::vector<std::string> strings; | |
for (size_t i = 0; i < 8000; ++i) | |
{ | |
if (i % 100 == 0) cout << "===== Iter " << i << endl; | |
char * buf2 = INIT(sz); | |
POSIX_MEMALIGN((void**)&buf2, 64, sz); | |
if (i % 100 == 0) cout << (void*)buf << " ; " << (void*)buf2 << endl; | |
memset(buf2, 0, sz); | |
free(buf); | |
buf = buf2; | |
strings.push_back(std::string("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
delay(); | |
if (i % 100 == 0) malloc_stats(); | |
} | |
free(buf); | |
} | |
#else | |
#include <opencv2/core/mat.hpp> | |
int main() | |
{ | |
cv::Size size(sz, 1); | |
cv::Mat tmp(size, CV_8UC1, cv::Scalar(0, 0, 0)); | |
std::vector<std::string> strings; | |
for (size_t i = 0; i < 2000; ++i) | |
{ | |
if (i % 100 == 0) cout << "===== Iter " << i << endl; | |
cv::Mat tmp2(size, CV_8UC1, cv::Scalar(0, 0, 0)); | |
if (i % 100 == 0) cout << (void*)tmp.data << " ; " << (void*)tmp2.data << endl; | |
tmp = tmp2; | |
strings.push_back(std::string("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
delay(); | |
if (i % 100 == 0) malloc_stats(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment