@@ -41,22 +41,39 @@ class UnexpectedError(Exception):
4141if platform .system () == "Linux" :
4242 import resource
4343
44- # We set the memory limit to 85% of total system memory when no swap exists
44+ # We set the memory limit to 85% of total system memory + swap when swap exists
4545 swap_file_path = Path ("/proc/swaps" )
4646 swap_exists = swap_file_path .is_file ()
47+ swap_size = 0
48+
4749 if swap_exists :
4850 with swap_file_path .open ("r" ) as f :
49- swap_exists = len (f .readlines ()) > 1 # First line is header
51+ swap_lines = f .readlines ()
52+ swap_exists = len (swap_lines ) > 1 # First line is header
53+
54+ if swap_exists :
55+ # Parse swap size from lines after header
56+ for line in swap_lines [1 :]:
57+ parts = line .split ()
58+ if len (parts ) >= 3 :
59+ # Swap size is in KB in the 3rd column
60+ try :
61+ swap_size += int (parts [2 ]) * 1024 # Convert KB to bytes
62+ except (ValueError , IndexError ):
63+ pass
64+
65+ # Get total system memory
66+ total_memory = os .sysconf ("SC_PAGE_SIZE" ) * os .sysconf ("SC_PHYS_PAGES" )
67+
68+ # Add swap to total available memory if swap exists
69+ if swap_exists :
70+ total_memory += swap_size
5071
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 )
72+ # Set the memory limit to 85% of total memory (RAM plus swap)
73+ memory_limit = int (total_memory * 0.85 )
5774
58- # Set both soft and hard limits
59- resource .setrlimit (resource .RLIMIT_AS , (memory_limit , memory_limit ))
75+ # Set both soft and hard limits
76+ resource .setrlimit (resource .RLIMIT_AS , (memory_limit , memory_limit ))
6077
6178
6279def pytest_addoption (parser : Parser ) -> None :
0 commit comments