Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Use proc meminfo for available memory on Linux.

kishorenca6a2000ddce6
diff --git a/include/system_metrics.h b/include/system_metrics.h
index fda58fdd..6ea58dee 100644
--- a/include/system_metrics.h
+++ b/include/system_metrics.h
@@ -118,6 +118,23 @@ private:
}
}
+ static unsigned long linux_get_mem_available_bytes() {
+ std::string token;
+ std::ifstream file("/proc/meminfo");
+ while(file >> token) {
+ if(token == "MemAvailable:") {
+ unsigned long mem_kb;
+ if(file >> mem_kb) {
+ return mem_kb * 1000;
+ } else {
+ return 0;
+ }
+ }
+ }
+
+ return 0; // nothing found
+ }
+
public:
void get(const std::string & data_dir_path, nlohmann::json& result);
diff --git a/src/system_metrics.cpp b/src/system_metrics.cpp
index a0c6d01e..ac5412b7 100644
--- a/src/system_metrics.cpp
+++ b/src/system_metrics.cpp
@@ -29,7 +29,7 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
getrusage(RUSAGE_SELF, &r_usage);
result["memory_used_process_bytes"] = r_usage.ru_maxrss * 1000;
- uint64_t memory_free_bytes = 0;
+ uint64_t memory_available_bytes = 0;
uint64_t memory_total_bytes = 0;
#ifdef __APPLE__
@@ -42,7 +42,7 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
if (KERN_SUCCESS == host_page_size(mach_port, &mach_page_size) &&
KERN_SUCCESS == host_statistics64(mach_port, HOST_VM_INFO,
(host_info64_t)&vm_stats, &count)) {
- memory_free_bytes = (int64_t)(vm_stats.free_count) * (int64_t)mach_page_size;
+ memory_available_bytes = (int64_t)(vm_stats.free_count) * (int64_t)mach_page_size;
}
uint64_t pages = sysconf(_SC_PHYS_PAGES);
@@ -51,11 +51,11 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
#elif __linux__
struct sysinfo sys_info;
sysinfo(&sys_info);
- memory_free_bytes = sys_info.freeram;
+ memory_available_bytes = linux_get_mem_available_bytes();
memory_total_bytes = sys_info.totalram;
#endif
- result["memory_free_bytes"] = memory_free_bytes;
+ result["memory_available_bytes"] = memory_available_bytes;
result["memory_total_bytes"] = memory_total_bytes;
// CPU METRICS