Skip to content

Commit e174ea9

Browse files
committed
more accurate memory estimate
1 parent 6874c75 commit e174ea9

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

+stdlib/checkRAM.m

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,36 @@
1313

1414
% get available RAM
1515
freebytes = stdlib.ram_free();
16+
17+
% get user set preference for memory limit
18+
s = settings;
19+
ws = s.matlab.desktop.workspace;
20+
21+
% Check if the maximum array size limit is enabled
22+
if ws.ArraySizeLimitEnabled.ActiveValue
23+
limit_bytes = double(ws.ArraySizeLimit.ActiveValue) / 100 * stdlib.ram_total();
24+
else
25+
limit_bytes = inf; % no limit set
26+
end
27+
28+
limit_bytes = min(limit_bytes, freebytes);
29+
1630
% variable sizing
1731
switch(myclass)
32+
case 'complex single', bits = 64;
33+
case 'complex double', bits = 128;
1834
case {'single','int32','uint32'}, bits = 32;
1935
case {'double','int64','uint64','float'}, bits = 64;
2036
case {'int16','uint16'}, bits = 16;
2137
case {'int8','uint8'}, bits = 8;
2238
case {'logical','bool'}, bits = 1;
23-
case {'string','char'}, bits = 8; % FIXME is this correct?
39+
case {'string','char'}, bits = 8;
2440
otherwise, error('unhandled variable class: %s', myclass)
2541
end
2642

2743
newSizeBytes = prod(newSize)*bits / 8;
2844

29-
OK = newSizeBytes < freebytes;
45+
OK = newSizeBytes < limit_bytes;
3046

3147
end
3248

+stdlib/ram_free.m

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%% RAM_FREE get free physical RAM
2-
% requires: java
2+
% optional: java
33
%
44
% get free physical RAM across operating systems
55
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getFreeMemorySize()
@@ -9,12 +9,23 @@
99

1010
function freebytes = ram_free()
1111

12-
b = javaOSBean();
12+
try
13+
% memory() was added cross-platform to Octave ~ 2021.
14+
% Matlab memory() at least through R2025a is still Windows only.
15+
m = memory();
16+
freebytes = m.MemAvailableAllArrays;
17+
catch e
18+
switch e.identifier
19+
case {'MATLAB:memory:unsupported', 'Octave:undefined-function'}
20+
b = javaOSBean();
1321

14-
if stdlib.java_api() < 14
15-
freebytes = b.getFreePhysicalMemorySize();
16-
else
17-
freebytes = b.getFreeMemorySize();
22+
if stdlib.java_api() < 14
23+
freebytes = b.getFreePhysicalMemorySize();
24+
else
25+
freebytes = b.getFreeMemorySize();
26+
end
27+
otherwise, rethrow(e)
28+
end
1829
end
1930

2031
end

0 commit comments

Comments
 (0)