Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Instrumentation Newsletter
NI Supported: Yes
Publish Date: Apr 2, 2008


Feedback


Yes No

Related Links - Developer Zone

Related Links - Products and Services

Memory Management in LabVIEW 8.5

0 ratings | 0.00 out of 5
Read in | Print

One advantage of graphical programming with NI LabVIEW software is the automatic handling of memory management.

However, there are cases when you may benefit from more control of memory usage in LabVIEW, such as performance-sensitive applications designed to run on a real-time system.


[+] Enlarge Image

Figure 1. This VI uses an array of complex data to illustrate an example program that could be optimized with the In Place Element Structure to eliminate copies of data in memory.

Allocating memory consumes processor time and introduces jitter, which can adversely affect performance. Real-time control is commonly found in systems that involve precise timing and high reliability, requiring you to consider how you manage resources such as memory. Memory considerations can also affect some desktop applications processing extremely large or complex sets of data, such as high-resolution images. To address scenarios such as these, LabVIEW 8.5 introduces the In Place Element Structure, which you can combine with good programming practices to manage memory and improve performance.

Default Memory Management by the LabVIEW Compiler

Since the introduction of LabVIEW more than 20 years ago, National Instruments has invested significantly in the LabVIEW compiler. This investment has helped minimize memory usage and optimize performance. However, the compiler simulates the run-time performance of a VI, which may not address every possible scenario or requirement for resources.  

When the compiler cannot assess the need to allocate new memory, it errs on the side of caution by creating a new copy in memory. In these instances, copying the data can cause execution timing delays and an overall increase in memory usage. With the new In Place Element Structure, you can notify the compiler that it is not necessary to copy data, thus providing more low-level control than previously possible.

Modifying an element within an array is a common routine in LabVIEW that you can optimize using this structure. This routine requires three programming steps:

1. Index the array and extract the element

2. Perform the modification to the element data

3. Reinsert the data into the array at the same index

The final step is performed using a copy of the array because the LabVIEW compiler does not know if the unmodified data is needed again. This is an example of performing an operation on a complex data type, such as an array or cluster. The output from these operations is stored in a new memory location, which means that it is no longer “in place.”

Profiling Application Memory Usage

On a desktop PC, you can examine the memory use of a VI using the “Performance and Memory Profile” window in the LabVIEW tools menu. Open the “Performance and Memory Profile” tool alongside the VI that you want to analyze. Select the VI metrics you want to view, such as memory, and click the “Start” button. The profiler is now running and monitoring your VI in memory. Run the VI you wish to examine. Once execution of the VI completes, click the “Stop” button in the profiler and watch it populate with the results.

The LabVIEW Real-Time Module comes with the Real-Time System Manager, which you can use to profile the performance of an application running on a real-time target. Select Tools >> Real-Time Module >> System Manager, and specify the IP address of the real-time target you want to analyze. Clicking on the resources tab displays information about total memory and CPU usage while running VIs. NI also offers the Real-Time Execution Trace Toolkit for profiling various advanced application metrics for individual threads, including memory.

If the memory consumption is large, or if performance on a real-time system appears to suffer from jitter, consider optimizing any operations performed on complex data types using the In Place Element Structure.


[+] Enlarge Image

Figure 2. This VI accomplishes the same task as the code in Figure 1, but it uses the In Place Element Structure to reduce the amount of memory the LabVIEW compiler allocates.

Controlling Memory Allocation with the In Place Element Structure

Operations performed inside the In Place Element Structure act on pointers to data; therefore, they do not create a copy of the data in a new memory location.

In Figure 1, the control labeled “Array” is an array of clusters, which contains arrays. If you want to increment a single number inside a nested array, you must implement the procedural steps shown on the block diagram. In this example, if the nested array has a large number of elements, this causes a costly duplication of data.

The element from the top-level array is extracted and reinserted to the same index, and the same is true of the array contained in the cluster and the element that is removed from it. The compiler cannot recognize that it does not need to create a copy in memory for the operations performed in Figure 1 even though you can. You can now tell the compiler to modify data “in place” by replacing these pairs of operations using the In Place Element Structure, as shown in Figure 2. Note that the structures can be nested and different types of border nodes are visible. You can right-click on the structure to see the menu for selecting the appropriate border node.

If the nested structure contained 10,000 numeric doubles, the use of the In Place Element Structure saves about 80 kB, or roughly the size of 10,000 copies of an 8 B value. All data operations in this example are performed “in place” without any data duplication.  

LabVIEW automatically handles memory management; however, the In Place Element Structure is an advanced tool for developers who want to further optimize memory usage. It is particularly useful for real-time applications and large sets of complex data.

Elijah Kerry

Elijah Kerry is a LabVIEW product engineer. He holds a bachelor’s degree in computer engineering from the University of Missouri – Columbia.

View a tutorial on using the In Place Element Structure.

 

LabVIEW Champions – Raising the Bar

LabVIEW Champions are an elite group of professors, industry professionals, and National Instruments Alliance Partners who raise the levels of LabVIEW knowledge and passion in the community. These evangelists contribute regularly to NI Discussion Forums online, lead user groups, and communicate directly with the LabVIEW R&D team.

Learn more or apply to become a LabVIEW Champion.

0 ratings | 0.00 out of 5
Read in | Print

Reader Comments | Submit a comment »

 

Legal
This material is protected under the copyright laws of the U.S. and other countries and any uses not in conformity with the copyright laws are prohibited, including but not limited to reproduction, DOWNLOADING, duplication, adaptation and transmission or broadcast by any media, devices or processes.