1111import sys
1212import time
1313import warnings
14+ from pathlib import Path
1415from typing import TYPE_CHECKING , Any , Callable
1516from unittest import TestCase
1617
@@ -37,17 +38,25 @@ class UnexpectedError(Exception):
3738 pass
3839
3940
40- if platform .system () == "Linux" or platform . system () == "Darwin" :
41+ if platform .system () == "Linux" :
4142 import resource
4243
43- # Get total system memory
44- total_memory = os .sysconf ("SC_PAGE_SIZE" ) * os .sysconf (
45- "SC_PHYS_PAGES"
46- ) # Set memory limit to 80% of total system memory
47- memory_limit = int (total_memory * 0.8 )
48-
49- # Set both soft and hard limits
50- resource .setrlimit (resource .RLIMIT_AS , (memory_limit , memory_limit ))
44+ # We set the memory limit to 85% of total system memory when no swap exists
45+ swap_file_path = Path ("/proc/swaps" )
46+ swap_exists = swap_file_path .is_file ()
47+ if swap_exists :
48+ with swap_file_path .open ("r" ) as f :
49+ swap_exists = len (f .readlines ()) > 1 # First line is header
50+
51+ if not swap_exists :
52+ # Get total system memory
53+ total_memory = os .sysconf ("SC_PAGE_SIZE" ) * os .sysconf (
54+ "SC_PHYS_PAGES"
55+ ) # Set the memory limit to 85% of total system memory
56+ memory_limit = int (total_memory * 0.85 )
57+
58+ # Set both soft and hard limits
59+ resource .setrlimit (resource .RLIMIT_AS , (memory_limit , memory_limit ))
5160
5261
5362def pytest_addoption (parser : Parser ) -> None :
0 commit comments