0

Update: Check accepted solution. Problem was irrelevant to class properties.

I have following problem in Matlab and cannot find anything on google or here. I have a handle class like this:

classdef myClass < handle
  properties
    hugeCellArray
    otherVariables
  end
  ...
end

I instanciate it as myObj = myClass(data); Now if I try: clear myObj.hugeCellArray it doesn't clear the property at all. If I try myObj.hugecellArray = []; it does set the property as [] but the memory is still allocated! So it seems like I cannot get rid of a huge variable in a class unless I clear the whole class? Thanks in advance!

Edit: (Clarification) My problem is the memory. I don't want to actually delete the class property, I want to free its memory.

Stefan
  • 414
  • 1
  • 6
  • 17
  • what do you mean by "memory is still allocated"? – Shai Jun 26 '13 at 13:03
  • Matlab is still using ~10GB. Meaning it has not freed up the memory (?). Meaning that if I call another function that needs memory I am going to run out of memory and start writing to swap. – Stefan Jun 26 '13 at 13:06
  • 1
    are you sure you dont have a closure or something such that there are still references to the property somewhere? setting it to an empty array should reclaim its memory.. Another explanation is that your memory became too fragmented – Amro Jun 26 '13 at 13:07
  • Actually I notice now that even `clear all` and `close all` don't free up the memory. I need to restart matlab to get it back – Stefan Jun 26 '13 at 13:12
  • It would make sense maybe as a fragmentation issue considering that the hugeCellArray is a cell array with 1600 matrices with quite large dimensions (1000x500). But is there seriously no way around this? – Stefan Jun 26 '13 at 13:16
  • 1
    If it is, i'm afraid there is little you can do once memory becomes too fragmented other than restart MATLAB.. For example see: http://stackoverflow.com/q/3300161/97160 , http://stackoverflow.com/q/16201005/97160 – Amro Jun 26 '13 at 13:28
  • So if instead of a cell array I concatenated everything into one huge array and kept indices of where each matrix started it would work fine? – Stefan Jun 26 '13 at 13:29
  • 1
    i think so yes. Allocating large chunks instead should alleviate the problem. Remember that MATLAB requires contiguous memory for numeric data. – Amro Jun 26 '13 at 13:32
  • What do you mean by "requires contiguous memory for numeric data"? The matrices I have stored in the cell array are in a sense independent of each other and concatenating them together in my case would require to keep indices of where each starts and ends. – Stefan Jun 26 '13 at 13:35
  • Have you looked at this question: [Cannot Update Class Definition In Matlab](http://stackoverflow.com/questions/17242237/cannot-update-class-definition-in-matlab), it may help your memory issue. – macduff Jun 26 '13 at 14:30
  • @Stefan: i was referring to the fact that cell arrays store each cell independently in memory while matrices are stored as one sequential block, but I think you already know this :) The key point here is to avoid many small allocations (or similarly growing a matrix repeatedly), so yes your solution sounds reasonable.. – Amro Jun 26 '13 at 16:21
  • @Stefan: you could write your conclusion as an answer below and mark it as accepted. – Amro Jun 26 '13 at 16:41

3 Answers3

4

You cant "delete" a property of an object without clearing the entire object itself.

If your concern is about memory, then what you tried does indeed clear the allocated memory:

myObj = myClass();
myObj.prop = rand(5000);
memory
myObj.prop = [];
memory
Amro
  • 123,847
  • 25
  • 243
  • 454
  • I don't have "memory" command under linux so I cannot prove it but looking at the memory used by matlab in the linux process manager before and after this command it uses the exact same amount of memory. – Stefan Jun 26 '13 at 13:10
  • oh sorry, i forgot that `memory` only works on Windows. perhaps you can try suggestions from here: http://stackoverflow.com/q/4762044/97160 – Amro Jun 26 '13 at 13:12
  • It works in my matlab on Win. I can see that memory is freed also using windows Task Manager. – Vit Bernatik Mar 12 '14 at 16:29
1

Solution by Amro:

Apparently the problem seems to be in cell arrays. They fragment my memory and it cannot be reused afterwards. Only workaround seems to be concatenating the cell array into one huge array and keeping tabs of where each subarray originally started.

Stefan
  • 414
  • 1
  • 6
  • 17
0

Maybe you want to find the answer like:

d.a = 3;
d.b = 4;
c = 3;
clear d.a

Both d.b and c still exist.

Davidietop
  • 31
  • 1
  • 3