Memory Allocation for Local and Global Variables in LabVIEW

Updated Dec 16, 2020

Reported In

Software

  • LabVIEW Base
  • LabVIEW Full

Issue Details

I am writing a LabVIEW application and I need to know when global and local variables are allocated and deallocated for memory management purposes. Is there a way to programmatically deallocate this memory?

Solution

Local Variables

The allocation of Local Variables occurs when the calling VI is loaded into memory. If it is a stand-alone VI, then the memory for the Local Variable is allocated at run-time and deallocated at the end of its run. If the Local Variable is declared in a subVI statically loaded in a main VI, then the memory for the Local Variable is allocated when the main VI is loaded into memory and deallocated by the LabVIEW memory manager shortly after the main VI is unloaded. If the subVI is loaded dynamically, as in the image below, then the memory for the Local Variable is allocated at the call of the subVI and deallocated by the LabVIEW memory manager shortly after the subVI is unloaded.
 

Global Variables

Global variables exist in special Global VIs, separate from the flow of the application. When accessed by another VI, they bring a copy of the variable into memory. This copy is destroyed when the calling VI is unloaded from memory. Note that copying large arrays or other large variables can be intensive and time consuming.

Deallocate Memory

There is no way in LabVIEW to programmatically deallocate the memory set aside for global and local variables, but you can deallocate the memory set aside for running a subVI before the top level VI has completed. Implement the Request Deallocation VI after the subVI has completed its operation to clear the subVI's memory.

Additional Information

The FP.Open property node variant is no longer supported since LabVIEW 2012. Instead of the property node, it needs to use the FP.Open Invoke node can be used to achieve the same function like below.