Memory leak from plotting?
12 views (last 30 days)
Show older comments
In Ubuntu 12.04 64-bit, matlab 2014a, I'd like to be able to plot thousands of lines over several minutes on the same plot (there are several plots total), without running out of memory. Currently, matlab slows way down after about ten seconds as more lines are added. It never crashes but just bogs down.
I can use 'cla', which clears all the axes and also removes prior drawn graphics objects. Same with 'cla reset' which removes objects and resets axes. Is there any way to accumulate many graphics objects without using up too much memory, eg turn it into a bitmap etc? Basically i want to see the evolution of the plots dynamically.
0 Comments
Answers (2)
dpb
on 7 Aug 2014
Don't keep making new objects; there's where the overhead is. See the doc's for guidelines on animating plots at http://www.mathworks.com/help/matlab/creating_plots/animation.html#brb6lra
0 Comments
John
on 7 Aug 2014
3 Comments
dpb
on 8 Aug 2014
Edited: dpb
on 8 Aug 2014
Hmmmm....I thought could do the following--
hL=plot(rand(1,10); % a line
set(h,'ydata',[get(h,'ydata');rand(1,10)])
and get second set for essentially free. But, I was remembering wrong, sorry.
Guess best you can do is to create new line handle for every line and that undoubtedly will begin to slow stuff down as the number increases. Least overhead I know would be
plot(x,y);
hold on
line(x2,y2)
Saving the handles would let you then keep some number and then use a circular buffer to only keep the N latest or the like...
ADDENDUM
A thought; don't know how well it would work in practice -- try allocating an array of LxN where L is length of each series and N is number of lines you think you want to try to plot. Use nan(L,N) for this instead of zeros, however. Then, before the first call to plot, fill the first column with "real" data and plot the array. The data will be allocated in the XYZData arrays, but the NaN values won't be visible. THEN, however, you can populate the next column of the handle arrays each subsequent call.
plot will still create the N line handles and if N is too big you may simply be at the limits of what you can do with Malab handle graphics, but the rest should be as little overhead as is possible if you can get the one initial creation step completed.
BTW, be sure you're not plotting way more data than can possibly be resolved on the screen -- if you've got a big monitor you've probably still got 2000 or so pixels, max. So, if you're plotting much more than that, only a diminishing fraction can be displayed uniquely, the rest are all mushed on top of each other. The moral is decimate to about the same length as there are number of pixels in the display to maximize the information viewable at minimum memory overhead.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!