Skip to content

Commit e0b7a94

Browse files
committed
test:ram_total parameter fun
1 parent 5b3ae11 commit e0b7a94

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

+stdlib/+dotnet/ram_total.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function bytes = ram_total()
2+
% .NET is 2-3x faster than Java for this
3+
% https://learn.microsoft.com/en-us/dotnet/api/system.gcmemoryinfo.totalavailablememorybytes
4+
bytes = System.GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;
5+
end

+stdlib/+java/ram_total.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function bytes = ram_total()
2+
3+
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getTotalMemorySize()
4+
5+
b = javaMethod("getOperatingSystemMXBean", "java.lang.management.ManagementFactory");
6+
7+
if stdlib.java_api() < 14
8+
bytes = b.getTotalPhysicalMemorySize();
9+
else
10+
bytes = b.getTotalMemorySize();
11+
end
12+
13+
bytes = uint64(bytes);
14+
15+
end

+stdlib/ram_total.m

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111
bytes = 0;
1212

1313
if stdlib.dotnet_api() >= 6
14-
% .NET is 2-3x faster than Java for this
15-
% https://learn.microsoft.com/en-us/dotnet/api/system.gcmemoryinfo.totalavailablememorybytes
16-
bytes = System.GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;
14+
bytes = stdlib.dotnet.ram_total();
1715
elseif stdlib.has_java()
18-
bytes = ram_total_java();
16+
bytes = stdlib.java.ram_total();
1917
end
2018

2119
if bytes <= 0
@@ -27,17 +25,4 @@
2725
end
2826

2927

30-
function bytes = ram_total_java()
31-
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getTotalMemorySize()
32-
33-
b = javaMethod("getOperatingSystemMXBean", "java.lang.management.ManagementFactory");
34-
35-
if stdlib.java_api() < 14
36-
bytes = b.getTotalPhysicalMemorySize();
37-
else
38-
bytes = b.getTotalMemorySize();
39-
end
40-
41-
end
42-
4328
%!assert(ram_total()>0)

test/TestSys.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
fun = {stdlib.isoctave, stdlib.has_dotnet, ...
99
stdlib.has_java, stdlib.has_python}
1010
ram_free_fun = {@stdlib.ram_free, @stdlib.sys.ram_free, @stdlib.java.ram_free, @stdlib.python.ram_free}
11+
ram_total_fun = {@stdlib.ram_total, @stdlib.sys.ram_total, @stdlib.dotnet.ram_total @stdlib.java.ram_total}
1112
end
1213

1314

@@ -108,9 +109,10 @@ function test_cpu_arch(tc)
108109
tc.verifyGreaterThan(strlength(arch), 0, "CPU architecture should not be empty")
109110
end
110111

111-
function test_ram_total(tc)
112+
function test_ram_total(tc, ram_total_fun)
113+
is_capable(tc, ram_total_fun)
112114

113-
t = stdlib.ram_total();
115+
t = ram_total_fun();
114116
tc.verifyGreaterThan(t, 0)
115117
tc.verifyClass(t, 'uint64')
116118
end

0 commit comments

Comments
 (0)