Matlab: free memory of class objects

I recently wrote some code using Matlab OOP. In each class object, I store some measurement data as a property and define methods for evaluating it. When using an average dataset, one class object uses about 32 MB of memory. I am now writing a GUI that needs to handle these objects.

In the first step, I load a set of objects from a saved .mat file (about 200 objects, 2GB hard disk space) and store them in a descriptor structure. They fill up RAM and use about 6-7 GB on boot. It's not a problem.

But if I close the GUI, it seems that I cannot free the used memory. I've tried different approaches with no success.

Setting data fields to "dest" in the class destructor:

function delete(obj)
    obj.timeVector = [];
    obj.valueVector = [];
end

      

Trying to free it in shape_CloseRequestFcn:

function figure_CloseRequestFcn(hObject, eventdata, handles)
    handles.data = [];
    handles = rmfield(handles,'data');
    guidata(hObject,handles);
    clear handles;
    pack; %Matlab issues a warning, that pack could only 
          %be used from the command line, but that did
          %not work either
    delete(hObject);
end

      

Any ideas besides closing Matlab every time after working with the GUI?

+3


source to share


3 answers


I found the answer at Matlab Bug Reporting Center. Seems to exist since R2011b.

Summary

Storing objects in MAT files can lead to memory leaks and prevent the object class from being cleaned

Description

After saving an instance of the class "MyClass" in the MAT file, calls to explicit classes can lead to a warning:

Warning: there are objects of class MyClass. There is no way to clean up this class or any of its superclasses.



This warning persists even if you clear all instances of the class on the stage. A warning may occur for one MAT file format and not another.

Bypass

In some cases, switching to a different MAT file format may eliminate the warning.

http://www.mathworks.ch/support/bugreports/857319

Edit: I've tried older save formats, but that doesn't work either. I am getting "Error Closing File" ( http://www.mathworks.ch/matlabcentral/answers/18098-error-using-save-error-closing-file ). Therefore Matlab does not support persistence class objects. I will have to live with memory problems and restart Matlab after every GUI use.

+2


source


Based on your screenshots memory

, certain memory is not cleared. There is a small chance that you have found a fundamental flaw in Matlab's garbage collection, but it is much more likely that the ~ 6Gigs resident data is still available through some series of links. Based on personal experience, here are a few ways in which the memory that you thought was cleared is still available:

  • Timer objects: If one of the timer callback functions refers to this data (or a copy), then this data is still available. You need to call deleted(t)

    on this timer.

  • Constant Variables in Functions: I often cache data in a constant variable inside a function, this explicitly allows that data to be accessed in the future, so it won't get cleared. You need to call clear FUNCTIONNAME

    to clear bound constant variables.

  • In GUI objects, both in data and in callback functions: numbers and any constants must be cleared.

  • Any static methods or constant attributes in classes that can persist data. They can either be cleaned up separately within the class, or by force using clear CLASSNAME

    .



Some tips for finding an outdated data link (again, based on personal errors)

  • See the exact number of bytes lost after each call using the call x=memory;

    to get the exact number. Is this consistent? Is this the number you recognize? Sometimes I can find a leak when realizing that it is exactly 238263232 bytes, so the double array 29782904, which should be from the xyz function.

  • See which classes are actually being removed. In your function, delete(obj)

    add a detail screen or what objects will be removed and also by output, which is not the case. For a given non-removable object, where could it be a reference? You don't need to clear the data in the function delete(obj)

    as you do, Matlab should handle this for you. Use the function delete

    as a debugging tool instead .

+1


source


Matlab has a garbage collector so you don't need to manually manage memory. After you close the GUI, all memory will be freed except for what is in your workspace. You can clear the workspace variables using clear

.

One thing I noticed on Windows (not sure about other platforms) is that the Matlab GUI sometimes saves extra memory (maybe 100 MB, but not a few GB as you can see). Simple minimization and restoration of the GUI will free up this excess memory.

-1


source







All Articles