Skip to content

Commit fadfe97

Browse files
committed
ram uint64
1 parent 1f7c4bb commit fadfe97

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

+stdlib/ram_free.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@
66
%
77
%%% Outputs
88
% * freebytes: free physical RAM [bytes]
9+
%
10+
% This is done using Java on non-Windows platforms.
11+
% VisualBasic (needs Windows) is needed to do this with .NET, so we use builtin memory() on Windows.
912

10-
function freebytes = ram_free()
13+
function bytes = ram_free()
1114

1215
try
1316
% memory() was added cross-platform to Octave ~ 2021.
1417
% Matlab memory() at least through R2025a is still Windows only.
1518
m = memory();
16-
freebytes = m.MemAvailableAllArrays;
19+
bytes = uint64(m.MemAvailableAllArrays);
1720
catch e
1821
switch e.identifier
1922
case {'MATLAB:memory:unsupported', 'Octave:undefined-function'}
2023
b = javaOSBean();
2124

2225
if stdlib.java_api() < 14
23-
freebytes = b.getFreePhysicalMemorySize();
26+
bytes = b.getFreePhysicalMemorySize();
2427
else
25-
freebytes = b.getFreeMemorySize();
28+
bytes = b.getFreeMemorySize();
2629
end
30+
bytes = uint64(bytes);
2731
otherwise, rethrow(e)
2832
end
2933
end

+stdlib/ram_total.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@
99

1010
function bytes = ram_total()
1111

12-
b = javaOSBean();
12+
% .NET is about 10 times SLOWER than Java
13+
% if ispc() && ~stdlib.isoctave()
14+
% h = NET.addAssembly('Microsoft.VisualBasic');
15+
% ci = Microsoft.VisualBasic.Devices.ComputerInfo();
16+
% bytes = ci.TotalPhysicalMemory;
17+
% delete(h);
18+
% else
19+
b = javaOSBean();
1320

14-
if stdlib.java_api() < 14
15-
bytes = b.getTotalPhysicalMemorySize();
16-
else
17-
bytes = b.getTotalMemorySize();
18-
end
21+
if stdlib.java_api() < 14
22+
bytes = b.getTotalPhysicalMemorySize();
23+
else
24+
bytes = b.getTotalMemorySize();
25+
end
26+
bytes = uint64(bytes);
27+
% end
1928

2029
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getTotalMemorySize()
2130

test/TestJava.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ function test_is_parallel(tc)
7373
end
7474

7575

76-
function test_ram(tc)
77-
tc.verifyGreaterThan(stdlib.ram_total(), 0)
78-
tc.verifyGreaterThan(stdlib.ram_free(), 0)
79-
end
80-
81-
8276
function test_cpu_count(tc)
8377
tc.verifyGreaterThan(stdlib.cpu_count(), 0)
8478
end

test/TestSys.m

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,27 @@ function test_username(tc)
3030
tc.verifyGreaterThan(strlength(u), 0)
3131
end
3232

33-
3433
function test_cpu_arch(tc)
3534
tc.assumeTrue(ispc() || stdlib.has_java())
3635

3736
arch = stdlib.cpu_arch();
3837
tc.verifyGreaterThan(strlength(arch), 0)
3938
end
4039

40+
function test_ram(tc)
41+
tc.assumeTrue(ispc() || stdlib.has_java())
42+
43+
t = stdlib.ram_total();
44+
f = stdlib.ram_free();
45+
46+
tc.verifyGreaterThan(t, 0)
47+
tc.verifyGreaterThan(f, 0)
48+
49+
tc.verifyLessThanOrEqual(f, t)
50+
51+
tc.verifyClass(t, 'uint64')
52+
tc.verifyClass(f, 'uint64')
53+
end
54+
4155
end
4256
end

0 commit comments

Comments
 (0)