@@ -9,36 +9,36 @@ if (PHP_INT_SIZE < 5) {
9
9
if (getenv ('SKIP_SLOW_TESTS ' )) {
10
10
die ('skip slow test ' );
11
11
}
12
- function get_system_memory ( ): int | float | false
12
+ function has_enough_memory ( int $ bytes ): bool
13
13
{
14
14
if (strtoupper (substr (PHP_OS , 0 , 3 )) === 'WIN ' ) {
15
15
// Windows-based memory check
16
16
@exec ('wmic OS get FreePhysicalMemory ' , $ output );
17
17
if (isset ($ output [1 ])) {
18
- return ((int )trim ($ output [1 ])) * 1024 ;
18
+ return ((( int )trim ($ output [1 ])) * 1024 ) >= $ bytes ;
19
19
}
20
20
} else {
21
- // check if running in docker
22
- $ cgroups = @file_get_contents ("/proc/self/cgroup " );
23
- if ($ cgroups && str_contains ($ cgroups , "docker " )) {
24
- $ limit = @file_get_contents ('/sys/fs/cgroup/memory/memory.limit_in_bytes ' );
25
- if ($ limit ) {
26
- return (int ) $ limit ;
27
- }
28
- } else {
29
- // Unix/Linux-based memory check
30
- $ memInfo = @file_get_contents ("/proc/meminfo " );
31
- if ($ memInfo ) {
32
- preg_match ('/MemFree:\s+(\d+) kB/ ' , $ memInfo , $ matches );
33
- return $ matches [1 ] * 1024 ; // Convert to bytes
34
- }
21
+ // cgroup v1
22
+ $ limit = @file_get_contents ('/sys/fs/cgroup/memory/memory.limit_in_bytes ' );
23
+ if ($ limit && (int )$ limit < $ bytes ) {
24
+ return false ;
25
+ }
26
+ // cgroup v2
27
+ $ limit = @file_get_contents ('/sys/fs/cgroup/memory.max ' );
28
+ if ($ limit && !str_contains ($ limit , 'max ' ) && (int )$ limit < $ bytes ) {
29
+ return false ;
30
+ }
31
+ // Unix/Linux-based memory check
32
+ $ memInfo = @file_get_contents ("/proc/meminfo " );
33
+ if ($ memInfo ) {
34
+ preg_match ('/MemFree:\s+(\d+) kB/ ' , $ memInfo , $ matches );
35
+ return ($ matches [1 ] * 1024 ) >= $ bytes ; // Convert to bytes
35
36
}
36
37
}
37
38
return false ;
38
39
}
39
- $ memAvail = get_system_memory ();
40
- if ($ memAvail < 10 * 1024 * 1024 * 1024 ) {
41
- die ('skip Reason: Insufficient RAM (should be 10GB, is ' .$ memAvail .' bytes) ' );
40
+ if (has_enough_memory (10 * 1024 * 1024 * 1024 )) { // 10GB
41
+ die ('skip Reason: Insufficient RAM (should be 10GB) ' );
42
42
}
43
43
$ tmpfile = sys_get_temp_dir () . DIRECTORY_SEPARATOR . "file_get_contents_file_put_contents_5gb.bin " ;
44
44
$ tmpfileh = fopen ($ tmpfile , "wb " );
0 commit comments