Skip to content

Instantly share code, notes, and snippets.

@meftunca
Created April 30, 2023 12:07
Show Gist options
  • Save meftunca/bdef1e46413e43bc8688bb946e8bd715 to your computer and use it in GitHub Desktop.
Save meftunca/bdef1e46413e43bc8688bb946e8bd715 to your computer and use it in GitHub Desktop.
#include <cstdint>
#include <iostream>
#if defined(__linux__)
#include <sys/resource.h>
#elif defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
#elif defined(_WIN32)
#include <windows.h>
#include <psapi.h>
#else
#error "Unsupported platform"
#endif
namespace mem_utils {
uint64_t get_memory_usage();
uint64_t get_memory_usage() {
#if defined(__linux__)
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
return static_cast<uint64_t>(rusage.ru_maxrss) * 1024;
#elif defined(__APPLE__) && defined(__MACH__)
task_basic_info_data_t taskInfo;
mach_msg_type_number_t infoCount = TASK_BASIC_INFO_COUNT;
kern_return_t kernReturn = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&taskInfo, &infoCount);
if (kernReturn == KERN_SUCCESS) {
return taskInfo.resident_size;
} else {
return 0;
}
#elif defined(_WIN32)
PROCESS_MEMORY_COUNTERS_EX memInfo;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&memInfo, sizeof(memInfo));
return static_cast<uint64_t>(memInfo.WorkingSetSize);
#endif
}
} // namespace mem_utils
/*
int main(){
uint64_t memory_usage_before = mem_utils::get_memory_usage();
std::cout << "Döngü öncesi bellek kullanımı: " << memory_usage_before << " bytes" << std::endl;
int sum;
// Döngü burada başlar
for (int i = 0; i < 1000; ++i) {
// Döngü içindeki işlemler burada gerçekleştirilir
sum+=i;
}
// Döngü burada biter
uint64_t memory_usage_after = mem_utils::get_memory_usage();
std::cout << "Döngü sonrası bellek kullanımı: " << memory_usage_after << " bytes" << std::endl;
std::cout << "Döngü bellek kullanımı: " << (memory_usage_after - memory_usage_before) << " bytes" << std::endl;
return 0;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment