Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Release Allocated Memory

Subject: Release Allocated Memory

From: Jonathan

Date: 2 Apr, 2009 02:32:02

Message: 1 of 4

I am running into a memory management issue when loading images (dicomread) into Matlab (r2008a). Matlab loads the images into memory and then saves them into the workspace. If I delete the variable in the workspace, Matlab does not free up the memory used for loading the images. If I load more images (different images), the amount of memory used by Matlab increases more. However, if I load the same set of images, matlab does not allocate additional memory (obviously using the previously allocated space for the last time these images were loaded).

This poses a problem if I am trying to load several different sets of images into the workspace at different times and I don't want the images that I am done with (and who's references have been deleted) taking up available memory.

Is there any way to force matlab to clear the memory used to load the images once they are deleted from the workspace? I don't think that pack is going to work in this case because I am calling it from a GUI and pack is relatively slow since it loads, then saves all variables in the workspace.

Thanks,
Jonathan

Subject: Release Allocated Memory

From: Thomas Clark

Date: 9 Apr, 2009 13:29:01

Message: 2 of 4

Jonathan,

We could do with some code snippets to demonstrate what you're doing, and the things you have tried. However, A warning bell started ringing when reading your post:

In a straightforward m-file, this certainly sounds like unusual behaviour.

Firstly, have you tried the clearvars command?

Secondly, you mention that you're using a GUI. When using a GUI, you are using a number of different callback functions and main functions, operating at the same time. You can get tied up in knots thinking about the scopes of the functions (if you don't know about function scope, suggest you search the ML docs for definitions of function scope).
It could easily be that you're deleting variables during a callback function from the GUI, which might delete variables from the local function scope; but retain them in a wider scope - hence the memory space is not deallocated. This is especially true if you're storing them in the handles structure - if that is the case, try using the guidata() command after deleting the variables to ensure that the gui-wide version of handles is updated.

Just a stab in the dark, I hope it helps rather than confusing...

Tom Clark



"Jonathan " <suever@gatech.edu> wrote in message <gr1832$ki0$1@fred.mathworks.com>...
> I am running into a memory management issue when loading images (dicomread) into Matlab (r2008a). Matlab loads the images into memory and then saves them into the workspace. If I delete the variable in the workspace, Matlab does not free up the memory used for loading the images. If I load more images (different images), the amount of memory used by Matlab increases more. However, if I load the same set of images, matlab does not allocate additional memory (obviously using the previously allocated space for the last time these images were loaded).
>
> This poses a problem if I am trying to load several different sets of images into the workspace at different times and I don't want the images that I am done with (and who's references have been deleted) taking up available memory.
>
> Is there any way to force matlab to clear the memory used to load the images once they are deleted from the workspace? I don't think that pack is going to work in this case because I am calling it from a GUI and pack is relatively slow since it loads, then saves all variables in the workspace.
>
> Thanks,
> Jonathan

Subject: Release Allocated Memory

From: Josie

Date: 20 Oct, 2009 09:39:01

Message: 3 of 4

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <grkt6t$5e3$1@fred.mathworks.com>...
> Jonathan,
>
> We could do with some code snippets to demonstrate what you're doing, and the things you have tried. However, A warning bell started ringing when reading your post:
>
> In a straightforward m-file, this certainly sounds like unusual behaviour.
>
> Firstly, have you tried the clearvars command?
>
> Secondly, you mention that you're using a GUI. When using a GUI, you are using a number of different callback functions and main functions, operating at the same time. You can get tied up in knots thinking about the scopes of the functions (if you don't know about function scope, suggest you search the ML docs for definitions of function scope).
> It could easily be that you're deleting variables during a callback function from the GUI, which might delete variables from the local function scope; but retain them in a wider scope - hence the memory space is not deallocated. This is especially true if you're storing them in the handles structure - if that is the case, try using the guidata() command after deleting the variables to ensure that the gui-wide version of handles is updated.
>
> Just a stab in the dark, I hope it helps rather than confusing...
>
> Tom Clark
>
>
>
> "Jonathan " <suever@gatech.edu> wrote in message <gr1832$ki0$1@fred.mathworks.com>...
> > I am running into a memory management issue when loading images (dicomread) into Matlab (r2008a). Matlab loads the images into memory and then saves them into the workspace. If I delete the variable in the workspace, Matlab does not free up the memory used for loading the images. If I load more images (different images), the amount of memory used by Matlab increases more. However, if I load the same set of images, matlab does not allocate additional memory (obviously using the previously allocated space for the last time these images were loaded).
> >
> > This poses a problem if I am trying to load several different sets of images into the workspace at different times and I don't want the images that I am done with (and who's references have been deleted) taking up available memory.
> >
> > Is there any way to force matlab to clear the memory used to load the images once they are deleted from the workspace? I don't think that pack is going to work in this case because I am calling it from a GUI and pack is relatively slow since it loads, then saves all variables in the workspace.
> >
> > Thanks,
> > Jonathan

Hi Jonathan,

Have you solved the problem? Can you show me a fraction of the code, how you free up the memory when dealing with GUI?

Thanks,
Josie

Subject: Release Allocated Memory

From: brijendra kumar

Date: 20 Oct, 2009 12:39:02

Message: 4 of 4

"Jonathan " <suever@gatech.edu> wrote in message <gr1832$ki0$1@fred.mathworks.com>...
> I am running into a memory management issue when loading images (dicomread) into Matlab (r2008a). Matlab loads the images into memory and then saves them into the workspace. If I delete the variable in the workspace, Matlab does not free up the memory used for loading the images. If I load more images (different images), the amount of memory used by Matlab increases more. However, if I load the same set of images, matlab does not allocate additional memory (obviously using the previously allocated space for the last time these images were loaded).
>
> This poses a problem if I am trying to load several different sets of images into the workspace at different times and I don't want the images that I am done with (and who's references have been deleted) taking up available memory.
>
> Is there any way to force matlab to clear the memory used to load the images once they are deleted from the workspace? I don't think that pack is going to work in this case because I am calling it from a GUI and pack is relatively slow since it loads, then saves all variables in the workspace.
>
> Thanks,
> Jonathan

suppose you have used following command for loading image ...

filename = urlwrite(url,'tmp.gif');

then use following command to delete this at the end of program ...

CLEAN UP
%delete(filename);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us