MATLAB Answers

Major slowdown when clearing objects in the wrong order

1 view (last 30 days)
David K
David K on 27 Jan 2021
Edited: David K on 1 Apr 2021
I've built a set of custom classes that I use for accessing and analyzing our datasets. I've run into the problem that when I run a blanket clear or clearvars to clean up my workspace, this will often cause the terminal to freeze. I discovered through some trial and error that the freeze is just MATLAB taking a long time to clear the workspace, but for small datasets I can wait through it. I've also discovered that if I run clearvars -except dataObj and then clear dataObj, then the workspace variables are cleared with no problem.
I ran some experiments using the profiler to get a better understanding of what's happening. In it, I cleared three different objects in different orders and checked the profiler to see where the delays were happening. The first object is rawDataObj, which is a container for all of the raw data. The second two objects are depData1 and depData2, which both use the rawData object to generate more information. The two dependent data objects each contain a pointer to the raw data object, but not vice versa. The objects have the properties depData1.rawData, depData2.rawData, as well as depData2.depData1. There's a lot of nesting in the two dependent data classes, so the raw data object may be referenced many times within the dependent data.
In my experiment I ran a script with three statements: clear rawData; clear depData1; clear depData2. I rearranged the three statements and checked the profiler to see how it impacted the runtime. Here's what I measured, with rough numbers from the profiler. I've renamed the three variables R, D1, and D2 for spacing. The Order indicated the order in which the objects were cleared.
Order |R,D1,D2|R,D2,D1|D1,D2,R|D2,D1,R|D1,R,D2|D2,R,D1|
R time |0 |0 |0.1s |0.1s |0 |10s |
D1 time |0 |10s |0 |0 |0 |0 |
D2 time |10s |0 |0 |0 |10s |0 |
As you can see from the numbers, the last object cleared always takes the longest time. If the last object is the raw data object, then it doesn't take much time. If it's one of the dependent data objects, then it takes over 100 times as long.
So now my main question - why is this happening, and how do I set up my destructor definitions to make it so things clean up quickly, no matter in what order I delete them?

Answers (0)




Community Treasure Hunt

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

Start Hunting!