Possible memory leak when opening figures that are maximized (2021b)

29 views (last 30 days)
I'm running 2021b on an Intel i9 Mac (12.1 Monterey) with 16gb of memory.
For me, there seems to be a significant discrepancy between the memory that Matlab uses when I open a figure maximized vs. when not.
The difference is so big that 15ish figures render my machine sluggish and interacting with figures becomes very painful.
Here's some code that illustrates the issue. I "profiled" the code using the Mac Activity Monitor to check the memory that is being used by Matlab. Testcase a) seems to be doing just fine. Case b) uses up to 8 gb of memory and case c) slightly less. Notice, that I am only plotting 3 datapoints per figure.
There also seems to be a disproportionate increase in memory after about the fifth figure. After that, each figure adds more than 1gb of memory, whereas the first five figures behave as expected.
Is this a known issue? Any advice / ideas for how to prevent this from happening?
Thanks in advance!
function test_matlab_memory_issue()
% run one after another to see memory usage
%test_a() % this is okay
%test_b() % this is bad
%test_c() % this is bad, but not as bad as b
end
function test_a()
[x,y,nfigs] = close_clear_clc_get_data();
for i=1:nfigs
figure()
plot(x,y);
end
end
function test_b()
[x,y,nfigs] = close_clear_clc_get_data();
for i=1:nfigs
fig_handle = figure();
set(fig_handle, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1])
plot(x,y);
end
end
function test_c()
[x,y,nfigs] = close_clear_clc_get_data();
for i=1:nfigs
figure('WindowState','maximized')
plot(x,y);
end
end
function [x,y,nfigs] = close_clear_clc_get_data()
close all
clear all
clc
x = [1,2,3];
y = [1,2,3];
nfigs = 10;
end
  5 Comments
Keith Myescough
Keith Myescough on 22 Apr 2023
Edited: Keith Myescough on 22 Apr 2023
Is it known whether this is a new issue? I'm concerned we might be seeing the same since switching to Matlab R2022b (from R2020b) last month. We are running an application that generates a lot of figures and we are also using the approach with "set" similar to what is done in case b.
Given the size of the application and the interplay between various parts controling what is plotted, it is very hard (to impossible) for us to switch to a design pattern like case a or c.

Sign in to comment.

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 3 Feb 2024
Hello Chip,
It appears that you're experiencing high memory usage when generating multiple figures in MATLAB on your MacOS system and you're looking for a way to optimize the memory usage. One effective method could be by changing the renderer for figures from "OpenGL" renderer, which typically consumes more memory to a more efficient "painters" renderer for 2D plots.
The following code sets the default figure renderer to painters.
set(0, 'DefaultFigureRenderer', 'painters');
The higher memory usage you're seeing with MATLAB might be linked to how Java handles memory internally, as MATLAB utilizes Java for some graphic elements. Java creates a pixel buffer for rendering each axes, which is a demanding process and is only executed when the figure is displayed. Java saves memory by avoiding the rendering of images not currently on the screen or by eliminating old, hidden pixel buffers. Docking the figures can noticeably reduce memory usage, which can be done via the 'show plot tools and Dock figure' button or the 'Dock Figures' shortcut. However, undocking the figures into separate windows and accessing each one can cause the memory usage to rise more.
For other optimizations to improve graphics performance, refer to the following documentation -
I hope this information is beneficial to you.
Thanks

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!