@@ -338,12 +338,16 @@ static int monitor_swap_contexts_handler(sd_event_source *s, uint64_t usec, void
338338 * is only used to decide which cgroups to kill (and even then only the resource usages of its descendent
339339 * nodes are the ones that matter). */
340340
341- if (oomd_swap_free_below (& m -> system_context , 10000 - m -> swap_used_limit_permyriad )) {
341+ /* Check amount of memory free and swap free so we don't free up swap when memory is still available. */
342+ if (oomd_mem_free_below (& m -> system_context , 10000 - m -> swap_used_limit_permyriad ) &&
343+ oomd_swap_free_below (& m -> system_context , 10000 - m -> swap_used_limit_permyriad )) {
342344 _cleanup_hashmap_free_ Hashmap * candidates = NULL ;
343345 _cleanup_free_ char * selected = NULL ;
344346 uint64_t threshold ;
345347
346- log_debug ("Swap used (%" PRIu64 ") / total (%" PRIu64 ") is more than " PERMYRIAD_AS_PERCENT_FORMAT_STR ,
348+ log_debug ("Memory used (%" PRIu64 ") / total (%" PRIu64 ") and "
349+ "swap used (%" PRIu64 ") / total (%" PRIu64 ") is more than " PERMYRIAD_AS_PERCENT_FORMAT_STR ,
350+ m -> system_context .mem_used , m -> system_context .mem_total ,
347351 m -> system_context .swap_used , m -> system_context .swap_total ,
348352 PERMYRIAD_AS_PERCENT_FORMAT_VAL (m -> swap_used_limit_permyriad ));
349353
@@ -361,9 +365,12 @@ static int monitor_swap_contexts_handler(sd_event_source *s, uint64_t usec, void
361365 log_notice_errno (r , "Failed to kill any cgroup(s) based on swap: %m" );
362366 else {
363367 if (selected )
364- log_notice ("Killed %s due to swap used (%" PRIu64 ") / total (%" PRIu64 ") being more than "
368+ log_notice ("Killed %s due to memory used (%" PRIu64 ") / total (%" PRIu64 ") and "
369+ "swap used (%" PRIu64 ") / total (%" PRIu64 ") being more than "
365370 PERMYRIAD_AS_PERCENT_FORMAT_STR ,
366- selected , m -> system_context .swap_used , m -> system_context .swap_total ,
371+ selected ,
372+ m -> system_context .mem_used , m -> system_context .mem_total ,
373+ m -> system_context .swap_used , m -> system_context .swap_total ,
367374 PERMYRIAD_AS_PERCENT_FORMAT_VAL (m -> swap_used_limit_permyriad ));
368375 return 0 ;
369376 }
0 commit comments