Skip to content

Commit 6a11a1d

Browse files
committed
ram: streamline
1 parent a3c932a commit 6a11a1d

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

+stdlib/+java/ram_free.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
function bytes = ram_free()
22

3-
b = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
3+
try
4+
b = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
45

5-
if stdlib.java_api() < 14
6-
bytes = b.getFreePhysicalMemorySize();
7-
else
8-
bytes = b.getFreeMemorySize();
6+
if stdlib.java_api() < 14
7+
bytes = b.getFreePhysicalMemorySize();
8+
else
9+
bytes = b.getFreeMemorySize();
10+
end
11+
catch e
12+
javaException(e)
13+
bytes = uint64.empty;
914
end
1015

1116
bytes = uint64(bytes);

+stdlib/+java/ram_total.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

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

7-
b = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
7+
try
8+
b = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
89

9-
if stdlib.java_api() < 14
10-
bytes = b.getTotalPhysicalMemorySize();
11-
else
12-
bytes = b.getTotalMemorySize();
10+
if stdlib.java_api() < 14
11+
bytes = b.getTotalPhysicalMemorySize();
12+
else
13+
bytes = b.getTotalMemorySize();
14+
end
15+
catch e
16+
javaException(e)
17+
bytes = [];
1318
end
1419

1520
bytes = uint64(bytes);

+stdlib/ram_free.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,34 @@
88
%%% Inputs
99
% * backend: backend to use
1010
%%% Outputs
11-
% * freebytes: free physical RAM [bytes]
11+
% * i: free physical RAM [bytes]
1212
% * b: backend used
1313

1414

15-
function [bytes, b] = ram_free(backend)
15+
function [i, b] = ram_free(backend)
1616
arguments
1717
backend (1,:) string = ["java", "python", "sys"]
1818
end
1919

20-
o = stdlib.Backend(mfilename(), backend);
21-
bytes = o.func();
20+
i = uint64.empty;
2221

23-
b = o.backend;
22+
for b = backend
23+
switch b
24+
case 'java'
25+
i = stdlib.java.ram_free();
26+
case 'python'
27+
if stdlib.matlabOlderThan('R2022a'), continue, end
28+
i = stdlib.python.ram_free();
29+
case 'sys'
30+
i = stdlib.sys.ram_free();
31+
otherwise
32+
error("stdlib:ram_free:ValueError", "Unknown backend: %s", b)
33+
end
34+
35+
if ~isempty(i)
36+
return
37+
end
38+
end
2439

2540
% * VisualBasic (needs Windows) is needed to do this with .NET.
2641
% * builtin memory() on Windows includes swap. The user could do that themselves.

+stdlib/ram_total.m

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,34 @@
66
%%% Inputs
77
% * backend: backend to use
88
%%% Outputs
9-
% * bytes: total physical RAM [bytes]
9+
% * i: total physical RAM [bytes]
1010
% * b: backend used
1111

12-
function [bytes, b] = ram_total(backend)
12+
function [i, b] = ram_total(backend)
1313
arguments
1414
backend (1,:) string = ["java", "dotnet", "python", "sys"]
1515
end
1616

17-
o = stdlib.Backend(mfilename(), backend);
18-
bytes = o.func();
17+
i = uint64.empty;
1918

20-
b = o.backend;
19+
for b = backend
20+
switch b
21+
case 'dotnet'
22+
i = stdlib.dotnet.ram_total();
23+
case 'java'
24+
i = stdlib.java.ram_total();
25+
case 'python'
26+
if stdlib.matlabOlderThan('R2022a'), continue, end
27+
i = stdlib.python.ram_total();
28+
case 'sys'
29+
i = stdlib.sys.ram_total();
30+
otherwise
31+
error("stdlib:ram_total:ValueError", "Unknown backend: %s", b)
32+
end
33+
34+
if ~isempty(i)
35+
return
36+
end
37+
end
2138

2239
end

0 commit comments

Comments
 (0)