Skip to content

Commit 05064ca

Browse files
committed
Try to reserve half of the memory under the limit
1 parent e2d5382 commit 05064ca

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/common/utils/memory_limit.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
# include <jemalloc/jemalloc.h>
3030
#endif
3131

32+
#if defined(SPADES_USE_MIMALLOC)
33+
extern "C" {
34+
void mi_collect(bool);
35+
size_t mi_stats_total_mem();
36+
int mi_reserve_os_memory(size_t size, bool commit, bool allow_large);
37+
void mi_debug_show_arenas();
38+
39+
};
40+
#endif
41+
3242
namespace utils {
3343

3444
void limit_memory(size_t limit) {
@@ -54,6 +64,15 @@ void limit_memory(size_t limit) {
5464
} else {
5565
INFO("Memory limit set to " << GB << " Gb");
5666
}
67+
68+
#if defined(SPADES_USE_MIMALLOC)
69+
// Reserve half of the limit memory
70+
res = mi_reserve_os_memory(rl.rlim_cur / 2, false, true);
71+
if (res != 0) {
72+
WARN("Failed to reserve OS memory of " << GB << " Gb, mi_reserve_os_memory() call failed, errno = "
73+
<< errno << " (" << strerror(errno) << "). Watch your memory consumption!");
74+
}
75+
#endif
5776
}
5877

5978
size_t get_memory_limit() {
@@ -88,13 +107,6 @@ size_t get_max_rss() {
88107

89108
#endif
90109

91-
#if defined(SPADES_USE_MIMALLOC)
92-
extern "C" {
93-
void mi_collect(bool);
94-
size_t mi_stats_total_mem();
95-
};
96-
#endif
97-
98110
size_t get_used_memory() {
99111
#if defined(SPADES_USE_JEMALLOC)
100112
// Update statistics cached by mallctl

0 commit comments

Comments
 (0)