23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Accessing Files with Memory-Mapping<br />

Deleting a Memory Map<br />

It is not necessary to explicitly call a destructor method to clear a memmapfile<br />

object from memory when you no longer need it. <strong>MATLAB</strong> calls the destructor<br />

for you whenever you do any of the following:<br />

• Reassign anothervaluetothememmapfile object’s variable<br />

• Clear the object’s variable from memory<br />

• Exit the function scope in which the object was created<br />

The Effect of Shared Data Copies On Performance<br />

When you assign the Data field of the memmapfile object to a variable,<br />

<strong>MATLAB</strong> makes a shared data copy of the mapped data. This is very efficient<br />

as no memory actually gets copied. In the following statement, memdat is a<br />

shared data copy of the data mapped from the file:<br />

memdat = m.Data;<br />

When you finish using the mapped data, make sure to clear any variables<br />

that shared data with the mapped file before clearing the object itself. If you<br />

clear the object first, then the sharing of data between the file and dependent<br />

variables is broken, and the data assigned to such variables must be copied<br />

into memory before the object is destroyed. If access to the mapped file was<br />

over a network, then copying this data to local memory can take considerable<br />

time. So, if the statement shown above assigns data to the variable memdat,<br />

you should be sure to clear memdat before clearing m when you are finished<br />

with the object.<br />

Note Keep in mind that the memmapfile object can be cleared in any of the<br />

three ways described under “Deleting a Memory Map” on page 6-69.<br />

Memory-Mapping Demo<br />

In this demonstration, two separate <strong>MATLAB</strong> processes communicate with<br />

each other by writing and reading from a shared file. They share the file by<br />

mapping part of their memory space to a commonlocationinthefile.Awrite<br />

operation to the memory map belonging to the first process can be read from<br />

the map belonging to the second, and vice versa.<br />

6-69

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!