@@ -41,22 +41,39 @@ class UnexpectedError(Exception):
41
41
if platform .system () == "Linux" :
42
42
import resource
43
43
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
45
45
swap_file_path = Path ("/proc/swaps" )
46
46
swap_exists = swap_file_path .is_file ()
47
+ swap_size = 0
48
+
47
49
if swap_exists :
48
50
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
50
71
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 )
57
74
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 ))
60
77
61
78
62
79
def pytest_addoption (parser : Parser ) -> None :
0 commit comments