Reduce Memory with Data References

Publish Date: Oct 13, 2015 | 20 Ratings | 3.00 out of 5 | Print

Overview

NI LabVIEW abstracts the need to manually manage memory when developing software. The LabVIEW compiler is always analyzing your code to determine how to optimize performance and reduce the amount of memory required. However, advanced users who want more control over memory allocation can create references to data in LabVIEW 2009.

Table of Contents

  1. Background
  2. Basic Example
  3. Using References with Classes
  4. Additional Resources

1. Background

Branching a wire typically requires that LabVIEW create a copy of the data on the wire. This is always true if both branches modify the value or if one modifies it before the other finishes using the original value. Use the "Show Buffer Allocations" option in the LabVIEW tools to highlight where the compiler will allocate additional memory.

Back to Top

2. Basic Example

The New Data Value Reference node takes any data as input and creates a reference to the data. Note that copying can still occur if the data is forked and used elsewhere.

Figure 1. The New Data Value Reference node creates a reference to data that you can use to transfer and access the data in a serialized way.

Two new nodes are available for the In Place Element Structure to operate on the data and replace it within the original memory location. Multiple In Place Element Structures can exist in the same VI, but only one can operate on the data from a reference at a time, as they are blocking functions to prevent race conditions. 

Figure 2. This VI Snippet illustrates an example of how this feature is used. To try this example in LabVIEW 2009, drag the image to the block diagram.

To help you understand this diagram, consider the individual components shown in this image:

  1. The value of the data for which a reference is created is five.
  2. The New Data Value Reference.vi creates a refnum for this bit of data.
  3. This is an example of what a constant looks like for a data reference.
  4. The In Place Element Structure has a pair of nodes for Data Value Reference Read/Write for dereferencing and rereferencing the data, respectively. This structure blocks the execution of other structures using the same reference until it is finished and the data has been rereferenced.
  5. The error out wire of the Data Value Reference Read node is "True" if the refnum is invalid. You will also receive this effort from the Write Value Reference Node, so you do not have to merge these errors. 
  6. The Delete Data Value Reference.vi ends the lifetime of the refnum and returns the last known value.
  7. This is an example of the terminal for the front panel indicator.

Error Handling

If you have multiple references that you need to simultaneously obtain, LabVIEW will wait at the structure boundary until all the references have been acquired. If any of the references are bad refnums, you have to merge all the errors together before you know whether to do any work.

Back to Top

3. Using References with Classes

You can use data references with any data type, including classes. LabVIEW classes have a hierarchical relationship, and references to classes mimic that hierarchy. In other words, a subVI that expects a parent class reference can be passed a child class reference.

Configuration options on a LabVIEW class give you the ability to restrict the creation and destruction of references to member VIs of the class. You can guarantee that any reference to the class is one that has been defined through the proper initialization method whenever you encounter that type on any other VI's diagram.

Figure 3. You can restrict the creation and deletion of references to classes from the properties dialog for a LabVIEW class.

If the class is marked that only member VIs can create references for the class, then the New Data Reference function simply breaks if you try to create a reference to the class type on any other VI's diagram – this is enabled by default. A reference to a cluster of the class or an array of the class cannot be restricted.

You must maintain the type of reference at runtime, which means that you cannot exchange data values. With a new Preserve Run-Time Class.vi primitive, LabVIEW can guarantee runtime type consistency that you can use to swap two values. If you need a class reference that lets you change the underlying object, create a reference to a cluster of the class. This gives you the ability to swap values at the cost of hierarchical casting.

Figure 4. The "Preserve Run-Time Class.vi" gives you the power to swap values of different references.

A second option allows a parent class to become the exclusive factory for references for all descendant types, which is disabled by default. That restriction works the same as the restriction for references to the class itself.

If you have a data refnum for a child class, you can use To More Generic.vi to upcast to a parent class reference. Likewise, you can later use To More Specific.vi to bring the reference back to the original type. Upcast coercion dots appear when you wire a ref-to-child-class wire to a ref-to-parent-class terminal. 

Back to Top

4. Additional Resources

Learn more about other new features in LabVIEW 2009

Back to Top

Bookmark & Share


Ratings

Rate this document

Answered Your Question?
Yes No

Submit