Thread Subject: Memory allocation

Subject: Memory allocation

From: Stephan

Date: 10 Sep, 2009 08:42:05

Message: 1 of 5

Hi there

I'm using Matlab for long time measurements (until a couple of days) with big data sets (image acquisition, ~26 MBytes/s). Using Matlab like this, you have to be very careful with memory allocation. Matlab is pretty sensitive in having memory leaks / filling the memory, because the program manages the memory allocation on its own.
If I'm acquiring image data, I want to allocate some memory. The memory I allocated in the variable "picture". My program looks like that:

function Main
...
% Init variable
picture = int8(zeros([vidObj.videoResolution(2) vidObj.videoResolution(1) 1 framesToRefresh+1]));
...
for i = 1:100
   dataPoints(i) = realtimeVideoAcquisition(picture);
end

function dataPoints = realtimeVideoAcquisition(picture)
...
[picture aqTimeTemp] = getdata(vid,framesAvaiable);
...

If I call the function "realtimeVideoAcquisition" like that, do I put the picture-data always into the same place of the memory or is the mamory always allocated new (provided that the returned data has always the same size)?

Subject: Memory allocation

From: dpb

Date: 10 Sep, 2009 13:17:56

Message: 2 of 5

Stephan wrote:
...
> If I'm acquiring image data, I want to allocate some memory.
> The memory I allocated in the variable "picture". My program looks
> like that:
>
> function Main
> ...
> % Init variable
> picture = int8(zeros([vidObj.videoResolution(2) vidObj.videoResolution(1) ...
> 1 framesToRefresh+1]));
> ...
> for i = 1:100
> dataPoints(i) = realtimeVideoAcquisition(picture);
> end
>
> function dataPoints = realtimeVideoAcquisition(picture)
> ...
> [picture aqTimeTemp] = getdata(vid,framesAvaiable);
> ...
>
> If I call the function "realtimeVideoAcquisition" like that, do I put
> the picture-data always into the same place of the memory or is the
> mamory always allocated new (provided that the returned data has
> always the same size)?

You've got a problem -- ML doesn't modify function arguments so if
realtimeVideoAcquisition(picture) is supposed to be returning the frame
in picture it won't, it would set a single value in the array
dataPoints() to something (or more likely, abort).

Also, you have an array and a function both named dataPoints; that'll
confuse matters most royally.

The memory allocated for picture will be the same but you're not using
it within allowable ML syntax. What it should be I don't know w/o the
documentation for the acquisition routines, but this can't be it...

--

Subject: Memory allocation

From: Stephan

Date: 10 Sep, 2009 15:16:23

Message: 3 of 5

dpb <none@non.net> wrote in message <h8auie$5o5$1@news.eternal-september.org>...
> Stephan wrote:
> ...
> > If I'm acquiring image data, I want to allocate some memory.
> > The memory I allocated in the variable "picture". My program looks
> > like that:
> >
> > function Main
> > ...
> > % Init variable
> > picture = int8(zeros([vidObj.videoResolution(2) vidObj.videoResolution(1) ...
> > 1 framesToRefresh+1]));
> > ...
> > for i = 1:100
> > dataPoints(i) = realtimeVideoAcquisition(picture);
> > end
> >
> > function dataPoints = realtimeVideoAcquisition(picture)
> > ...
> > [picture aqTimeTemp] = getdata(vid,framesAvaiable);
> > ...
> >
> > If I call the function "realtimeVideoAcquisition" like that, do I put
> > the picture-data always into the same place of the memory or is the
> > mamory always allocated new (provided that the returned data has
> > always the same size)?
>
> You've got a problem -- ML doesn't modify function arguments so if
> realtimeVideoAcquisition(picture) is supposed to be returning the frame
> in picture it won't, it would set a single value in the array
> dataPoints() to something (or more likely, abort).
>
> Also, you have an array and a function both named dataPoints; that'll
> confuse matters most royally.
>
> The memory allocated for picture will be the same but you're not using
> it within allowable ML syntax. What it should be I don't know w/o the
> documentation for the acquisition routines, but this can't be it...
>
> --

Ok, Matlab doesn't modify function argmunents. So how can you allocate space for variables for a function you use again and again? is there a possibility to do that? Because i think if Matlab is doing that on its own, it's doing some mess...
To the code: I don't want to return pictures. I want to load the picture data in the function "realtimeVideoAcquisition", make a calculation and return the values of the calculation in dataPoints.
By the way: There is no function called "dataPoints", "dataPoints" is the return value of the function realtimeVideoAcquisition...

Subject: Memory allocation

From: Bruno Luong

Date: 10 Sep, 2009 16:09:19

Message: 4 of 5


> Ok, Matlab doesn't modify function argmunents. So how can you allocate space for variables for a function you use again and again? is there a possibility to do that? Because i think if Matlab is doing that on its own, it's doing some mess...

Yes or no.

There is no way to prevent Matlab doing "mess" with memory. It's constantly loading function and allocate memory on its own. When you call a function GETDATA (as with any function that return an array), the memory is allocated to store the result and returned.

You can enforce the result to be in the same buffer, but contrary to the apparence the data is NEVER copied DIRECTLY into the same buffer. Indeed the function creates an temporary array internally, then copy the data to the output, then free the array. Example:

% If you call
Buffer = zeros(100); % allocate
Buffer(1:end) = peaks(100); % <- getdata

% The data will be located always at the same RAM address

% But the above is equivalent to:
Buffer = zeros(100); % allocate
peaks(100); % allocate a second array here internally, and pointed by ANS
Buffer(1:end) = ans; % <- copy data, then free the old data prior pointed by ans

In short, the above strategy SEEMS to copy over and over on the same buffer, but it does in fact more work than the simplest call:

Buffer = peaks(100);

So in conclusion, don't bother with complex allocation with Matlab. This is not possible. The only way to work around is to make a MEX file with static pointer, and access directly to your hardware device to get the data. In short : goes to lower language for total control.

% Bruno

Subject: Memory allocation

From: Stephan

Date: 14 Sep, 2009 09:24:02

Message: 5 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h8b8bf$o9m$1@fred.mathworks.com>...
>
> > Ok, Matlab doesn't modify function argmunents. So how can you allocate space for variables for a function you use again and again? is there a possibility to do that? Because i think if Matlab is doing that on its own, it's doing some mess...
>
> Yes or no.
>
> There is no way to prevent Matlab doing "mess" with memory. It's constantly loading function and allocate memory on its own. When you call a function GETDATA (as with any function that return an array), the memory is allocated to store the result and returned.
>
> You can enforce the result to be in the same buffer, but contrary to the apparence the data is NEVER copied DIRECTLY into the same buffer. Indeed the function creates an temporary array internally, then copy the data to the output, then free the array. Example:
>
> % If you call
> Buffer = zeros(100); % allocate
> Buffer(1:end) = peaks(100); % <- getdata
>
> % The data will be located always at the same RAM address
>
> % But the above is equivalent to:
> Buffer = zeros(100); % allocate
> peaks(100); % allocate a second array here internally, and pointed by ANS
> Buffer(1:end) = ans; % <- copy data, then free the old data prior pointed by ans
>
> In short, the above strategy SEEMS to copy over and over on the same buffer, but it does in fact more work than the simplest call:
>
> Buffer = peaks(100);
>
> So in conclusion, don't bother with complex allocation with Matlab. This is not possible. The only way to work around is to make a MEX file with static pointer, and access directly to your hardware device to get the data. In short : goes to lower language for total control.
>
> % Bruno

Hi Bruno

Thanks for this great reply. That's what I'm going to do now: I'm writing the image acquisition in C!

Cheers Stephan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
memory Sprinceana 10 Sep, 2009 09:52:29
allocation Sprinceana 10 Sep, 2009 09:52:29
memory allocation Sprinceana 10 Sep, 2009 09:52:29
rssFeed for this Thread

Contact us at files@mathworks.com