Out of Memory for using many global variables
Mostrar comentarios más antiguos
Hi
I have many functions. so instead of use the variables as function arguments to transfer values between functions. I set many variables as global variables. These global variables are not used all the time.
on the top of main function, I did use the following to clear the space.
clc clear all; close all; however, when opening a big excel file(200MB), I ran into out of memory issue for 2014b version. When I change it to use 2017 version. the issue was gone but loading speed for opening excel using xlread() is still very slow. Now I need your help on the following questions.
1. How to reduce memory space with many global variables? How to only assign space when it is used? 2. I have few toolboxes, like statistics, which are only linked to 2014b version. Is it possible to use these toolbox for 2017 version?
Thank you so much for your help. I appreciate it.
11 comentarios
"I have many functions. so instead of use the variables as function arguments to transfer values between functions. I set many variables as global variables."
I don't see why having many functions requires using global variables. You would likely be much better off passing the data as proper input/output arguments, rather than wasting time using slow and buggy globals.
"on the top of main function, I did use the following to clear the space. clc clear all; close all; "
Why? When a function is first called its workspace is empty (apart from any input arguments), so calling clear all serves no purpose whatsoever and is in fact counter-productive: it will clear all loaded functions and classes, which will then just have to be loaded again when the code is run, thus slowing down your code. It is highly unlikely that clc or close all serve any purpose actually related to the functionality of your functions, so what is the point of calling them? Of course apart from the usual reason why people call those everywhere:
On to your your questions:
"1. How to reduce memory space with many global variables? How to only assign space when it is used?"
Don't use globals.
Pass data properly between workspaces and trust the MATLAB engine to deal with memory and garbage collection.
"2. I have few toolboxes, like statistics, which are only linked to 2014b version. Is it possible to use these toolbox for 2017 version?"
Are these MATLAB toolboxes or third-party toolboxes?
Matt J
el 9 de Feb. de 2018
I have many functions. so instead of use the variables as function arguments to transfer values between functions.
Is this to conserve memory? You will only see a saving when passing variables to functions that actually modify those variables.
Adam
el 9 de Feb. de 2018
Toolboxes can only be used with the version of Matlab for which they are linked. If you upgrade base Matlab you have to upgrade toolboxes equivalently.
As for memory usage, goodness knows what is in memory and when if you have a vast number of global variables.
Functions define scope for variables and when the function ends the variable goes out of scope (putting aside handle-derived objects which you are likely not using) and the garbage collection will release the memory. If you are liberally creating lots of global variables though then they will sit around until you get rid of them.
roudan
el 9 de Feb. de 2018
Rik
el 9 de Feb. de 2018
- Use the callback of that dropdown and get(hObject,'Value') to call the correct function to plot your data.
- Don't use globals at all. But if you do use them, you indeed need to only call global for the variables you use in that function.
@Yongnuan Liu: you might like to try using nested functions, which are a very intuitive way to transfer data between callbacks and functions:
You can easily combine nested functions with local functions and pass output arguments too. Have a look at my FEX submission iregexp for an example of this:
Don't use globals.
roudan
el 9 de Feb. de 2018
Rik
el 9 de Feb. de 2018
If each function has access to the same figure, you can use guidata.
roudan
el 9 de Feb. de 2018
Greg
el 9 de Feb. de 2018
doc guidata
Walter Roberson
el 9 de Feb. de 2018
If you have enough data to be running out of memory, you should be using setappdata()/getappdata() instead of guidata(). guidata() is invoked by GUIDE for every callback, and a copy of all the stored data is created each time. With setappdata()/getappdata(), you can pull in copies of just the data you need.
Even then, since the data is large, you should probably instead rewrite your code to use nested functions with shared variables -- or create a handle object containing the array, possibly using https://www.mathworks.com/help/matlab/matlab_oop/handle-compatible-classes-and-heterogeneous-arrays.html
Respuestas (0)
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!