Clear Filters
Clear Filters

Matlab 'Reserved memory' ('MemUsedMatlab') creep

21 views (last 30 days)
I have built a Matlab app, using app designer, which runs in real-time. I monitor the 'MemUsedMatlab' parameter periodically i.e. the memory reserved for the Matlab process. Despite careful memory management within the app e.g. by using 'clear' to delete unwanted variables, and array pre-allocation, MemUsedMatlab continues to creep up. After several days of running, the high value of MemUsedMatlab significantly slows down the closure of the app - it can be 10's of mins before the 'play symbol' becomes active to allow a restart of the app. Even if I clear all app. variables (objects) by setting them to [], this does not clear the 'MemUsedMatlab' build up.
Does Matlab have a 'purge' command equivalent? If not, is there anyway I can clear the memory, reserved for the Matlab process?
Note, I have not used the profiler - can anyone point me to a good tutorial guide.
Thanks.

Accepted Answer

Hassaan
Hassaan on 10 Jan 2024
In MATLAB, memory management is mostly handled automatically, and there isn't a direct 'purge' command as you might find in some other environments. However, there are several strategies you can employ to manage memory usage:
  1. Garbage Collection: MATLAB performs garbage collection automatically. When variables are cleared or go out of scope, MATLAB will eventually reclaim that memory. The timing of this is not guaranteed, so memory may not be freed immediately after variables are cleared.
  2. clear Command: While you've mentioned using clear, it's worth noting that clear removes variables from the workspace but does not necessarily reduce the memory reported by MemUsedMatlab immediately as this is managed by MATLAB's garbage collector.
  3. pack Command: In earlier versions of MATLAB, the pack command was used to consolidate workspace memory. However, it is not recommended in newer versions as it can be disruptive and may not help in all situations.
  4. memory Function: You can use the memory function to get detailed information about the memory used by MATLAB and the system. This can help you to identify if the memory usage is within MATLAB or if it's related to the system memory.
  5. Optimizing Code: Use MATLAB's profiling tools to identify memory-intensive parts of the code. The Profiler can be accessed via profile viewer or profile on - it's a powerful tool to find bottlenecks in your code.
  6. Memory Leak Diagnosis: If memory usage keeps increasing, there may be a memory leak. This could be due to growing arrays, figures, or GUI components that are not being properly deleted. Ensure you're closing figures and freeing up GUI components or timers that are no longer needed.
  7. MATLAB Version: Ensure you are using the latest MATLAB version, as memory management is improved with each release.
  8. MATLAB Support: If you've tried all the above and still face issues, consider contacting MathWorks' support team. They can help diagnose such issues, especially if it's a bug or a problem with MATLAB itself.
  9. Restarting MATLAB: If memory usage becomes problematic, you may need to schedule periodic restarts of MATLAB. It's not ideal, but it can be a practical way to clear memory.
There isn't a simple tutorial for the profiler as it's a very extensive tool, but the MATLAB documentation provides a good starting point. You can access it by typing doc profile or help profile in the MATLAB Command Window. The MathWorks website also has resources and examples of using the Profiler.
Lastly, if you're sure the memory should be freed but MemUsedMatlab still shows high usage, it could be a sign of a memory leak within MATLAB's environment or within external libraries or toolboxes you are using. In such cases, a detailed investigation or consultation with MathWorks support might be necessary.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (0)

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!