Created
December 16, 2020 21:19
-
-
Save diablodale/189082bac1e244bba8906ba175a1f3e7 to your computer and use it in GitHub Desktop.
OpenCV allocator fragmentation tester
This file contains 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 <Windows.h> | |
#include <psapi.h> | |
#include <thread> | |
#include <utility> | |
#include <vector> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <iostream> | |
#include <malloc.h> | |
#include <cassert> | |
using namespace std; | |
const size_t sz = 3 * 1000 * 1000; | |
inline void delay() { std::this_thread::sleep_for(std::chrono::milliseconds(1)); } | |
const auto curProcess = GetCurrentProcess(); | |
// switch between raw malloc calls and OpenCV | |
#if 1 | |
// swtich between _aligned_malloc calls and malloc | |
#if 1 | |
#define MEMALIGN(a, b) (char*)_aligned_malloc((a), (b)) | |
#define FREEALIGN(a) _aligned_free((a)) | |
#else | |
#define MEMALIGN(a, b) (char*)malloc((a)) | |
#define FREEALIGN(a) free((a)) | |
#endif | |
int main() | |
{ | |
char * lastBuf = MEMALIGN(sz, 64); | |
memset(lastBuf, 0, sz); | |
std::vector<std::string> strings; | |
for (size_t i = 0; i < 8000; ++i) | |
{ | |
if (i % 100 == 0) cout << "===== Iter " << i << endl; | |
char * newBuf = MEMALIGN(sz, 64); | |
if (i % 100 == 0) cout << (void*)lastBuf << " ; " << (void*)newBuf << endl; | |
memset(newBuf, 0, sz); | |
FREEALIGN(lastBuf); | |
lastBuf = newBuf; | |
strings.push_back(std::string("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
//delay(); | |
if (i % 100 == 0) { | |
PROCESS_MEMORY_COUNTERS_EX memCounter; | |
BOOL result = GetProcessMemoryInfo( | |
curProcess, | |
(PROCESS_MEMORY_COUNTERS*)&memCounter, | |
sizeof(memCounter) | |
); | |
std::cout << "workingset " << memCounter.WorkingSetSize << std::endl; | |
std::cout << "commitcharge " << memCounter.PagefileUsage << std::endl; | |
assert(memCounter.PrivateUsage == memCounter.PagefileUsage); | |
std::cout << "peakcommitcharge " << memCounter.PeakPagefileUsage << std::endl; | |
} | |
} | |
FREEALIGN(lastBuf); | |
} | |
#else | |
#include <opencv2/core/mat.hpp> | |
int main() | |
{ | |
cv::Size size(sz, 1); | |
cv::Mat lastBuf(size, CV_8UC1, cv::Scalar(0, 0, 0)); | |
std::vector<std::string> strings; | |
for (size_t i = 0; i < 8000; ++i) | |
{ | |
if (i % 100 == 0) cout << "===== Iter " << i << endl; | |
cv::Mat newBuf(size, CV_8UC1, cv::Scalar(0, 0, 0)); | |
if (i % 100 == 0) cout << (void*)lastBuf.data << " ; " << (void*)newBuf.data << endl; | |
lastBuf = newBuf; | |
strings.push_back(std::string("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
//delay(); | |
if (i % 100 == 0) { | |
PROCESS_MEMORY_COUNTERS_EX memCounter; | |
BOOL result = GetProcessMemoryInfo( | |
curProcess, | |
(PROCESS_MEMORY_COUNTERS*)&memCounter, | |
sizeof(memCounter) | |
); | |
std::cout << "workingset " << memCounter.WorkingSetSize << std::endl; | |
std::cout << "commitcharge " << memCounter.PagefileUsage << std::endl; | |
assert(memCounter.PrivateUsage == memCounter.PagefileUsage); | |
std::cout << "peakcommitcharge " << memCounter.PeakPagefileUsage << std::endl; | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment