xff_-_memory_management
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | xff_-_memory_management [2010/06/10 13:34] (current) – created jochen | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== xff - memory management ====== | ||
+ | ===== Motivation ===== | ||
+ | Matlab' | ||
+ | |||
+ | <code matlab matlab_pass_by_value.m> | ||
+ | % bad coordinate values in variables X, Y, and Z with | ||
+ | % NaNs in all three of them | ||
+ | [X, Y, Z] = nanbadcoords(X, | ||
+ | |||
+ | While this is relatively convenient (and Matlab' | ||
+ | |||
+ | ===== Implementation ===== | ||
+ | In xff (and also the other NeuroElf classes, for that matter), storage is not allocated in the struct variable that constitutes the class object (which is true for most other non-NeuroElf class objects!). The only field in the struct variable of any xff object is the '' | ||
+ | |||
+ | <code matlab>% creating an object | ||
+ | vmr = xff(' | ||
+ | |||
+ | % displaying the struct contents: | ||
+ | struct(vmr)</ | ||
+ | |||
+ | This would produce something like the following sample output: | ||
+ | |||
+ | < | ||
+ | ans = | ||
+ | |||
+ | L: 0.6692 | ||
+ | </ | ||
+ | |||
+ | The actual storage is kept in a global variable, '' | ||
+ | |||
+ | <code matlab>% intialize xff | ||
+ | xff; | ||
+ | |||
+ | % display list of global variables | ||
+ | whos global</ | ||
+ | |||
+ | would produce (if no other global variables are present): | ||
+ | |||
+ | < | ||
+ | |||
+ | xffclup | ||
+ | xffconf | ||
+ | xffcont | ||
+ | xfflast | ||
+ | xinimeth | ||
+ | |||
+ | These variables have the following content/ | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * C - actual content (what is displayed when an object variable is used without a semicolon) | ||
+ | * F - filename (empty if not loaded from disk and not saved yet, "''< | ||
+ | * H - sub-struct of handles to other objects (e.g. transimg objects for data slicing, etc.) | ||
+ | * L - lookup value (for convenience stored also in the struct) | ||
+ | * S - file format specification (extension(s), | ||
+ | * U - unwind stack information (used for the internal automatic garbage collection of xff) | ||
+ | * xfflast - 1x2 double value, for which object was the fieldnames/ | ||
+ | * xinimeth - method name storage of xini class; as xff uses xini for some settings, this class is also initialized | ||
+ | |||
+ | ===== Consequences ===== | ||
+ | **As the storage is //NOT// directly associated with any given object variable, using Matlab' | ||
+ | |||
+ | <code matlab xff_clearobject_sample.m> | ||
+ | vmr = xff(' | ||
+ | |||
+ | % set random content | ||
+ | vmr.VMRData = uint8(100 + round(10 * randn(size(vmr.VMRData)))); | ||
+ | |||
+ | % save as (without argument requests for filename) | ||
+ | vmr.SaveAs; | ||
+ | |||
+ | % clear object!! | ||
+ | vmr.ClearObject;</ | ||
+ | |||
+ | Without this last line, the associated memory would remain allocated even if this is used in a sub-function (which removes the vmr object from memory, but that only contains a struct with the '' | ||
+ | |||
+ | ===== Garbage collection ===== | ||
+ | To ensure that user-written functions do not clutter up the global '' | ||
+ | |||
+ | * for any object, the stack is recorded during object creation (which function created the object) | ||
+ | * whenever any xff method is called, the stack of all remaining objects is checked, and if the current stack doesn' | ||
+ | * to protect objects from being affected, the '' | ||
+ | |||
+ | ===== bless ===== | ||
+ | Here is an example of a user-written function that creates an object which is suitable for passing outside of the function and not being affected by this garbage collection (it also creates another object which would be removed by garbage collection if the '' | ||
+ | |||
+ | <code matlab spheresvmr_sample.m> | ||
+ | % spheresvmr_sample | ||
+ | % | ||
+ | % FORMAT: | ||
+ | % | ||
+ | % No input fields. | ||
+ | % | ||
+ | % Output fields: | ||
+ | % | ||
+ | % | ||
+ | |||
+ | % create VMR object (output) | ||
+ | vmr = xff(' | ||
+ | |||
+ | % create VOI object as a helper | ||
+ | voi = xff(' | ||
+ | |||
+ | % create 10 spheres in VOI | ||
+ | for c = 1:10 | ||
+ | voi.AddSphericalVOI(min(120, | ||
+ | end | ||
+ | |||
+ | % set those coordinates to 200 in VMR | ||
+ | for c = 1:10 | ||
+ | vmr.VMRData(bvcoordconv(voi.VOI(c).Voxels, | ||
+ | end | ||
+ | |||
+ | % make sure VMR is not affected by garbage collection | ||
+ | bless(vmr, 1); | ||
+ | |||
+ | % destroy voi (would be removed by garbage collection some time later otherwise) | ||
+ | voi.ClearObject; | ||
+ | |||
+ | % end of function</ |
xff_-_memory_management.txt · Last modified: 2010/06/10 13:34 by jochen