LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Memory Usage of Accessing an Array using Local Variables

Hey people,

I've been reading up on efficient memory usage in this forum and the tutorials, especially with respect to arrays, since I'm using a few boolean arrays in my current project. I have some questions regarding this.

In my program (which is to be embedded into a FieldPoint controller) I have three parallel while loops. One of the loops regularly updates the value of 2 arrays (one of 8 boolean values, one of 16 boolean values) based on the values of a couple of controls, in other words, the 'logic' part of my VI. Another loop is responsible for accessing a DataSocket server in order to obtain the values based on which the arrays will be updated, while also writing the values contained within the arrays to the DataSocket server. The third is responsible for actually outputting the values contained within the arrays to the FieldPoint controller.

My question has to do with how copies of an array affect memory usage. From my readings so far it seems that pre-allocation of an array of constant size (my arrays do not resize) will reduce the amount of memory usage, as well as the usage of a shift register to pass the values of the array from one iteration of a while loop to the next, in place of using a local variable. However, for my 2nd and 3rd loops, both of which require the values of the arrays for output to either the DataSocket server or the FieldPoint controller, I am currently using local variables of the arrays to pass these values, and it works fine when the program is run on a desktop with 1 GB of RAM. However, since the FieldPoint unit only has 8 MB of DRAM, I would like to know how exactly these local variables affect my overall program's memory usage.

Will each local variable make a new copy of the array for every iteration of the loop it resides in, filling up the memory totally? Or will each local variable reserve an array in the RAM for itself (since it is of constant size) which it will then use for every iteration?

If I'm using a shift register for the first loop and the output of the left-side shift register is wired to two blocks (one Index Array to extract the values and one Replace Array Subset to replace the values after completion of the program logic) will this generate 2 copies of the array in memory? Will each new iteration then reserve a new slot in the memory for the new arrays?

I have read a few of the forum posts on related topics, and also referred to this tutorial

http://zone.ni.com/devzone/conceptd.nsf/webmain/732cec772aa4fbe586256a37005541d3

Thanks for the help!
0 Kudos
Message 1 of 4
(3,217 Views)
LabVIEW will allocate memory for each instance of the local variable and not everytime its read, so basically how many times you see the local variable on the block diagram (even if you read it in the loop that is only one instance).  I might suggest using one single while loop instead of multiple parallel loops to prevent race conditions, such as trying to read and write the local variable at the same time, from developing.  Just because you have three processes, doesn't necessarily mean that you should have three loops.  It would probably make just as much sense to combine these into one loop, since it doesn't sound like one process or another would take very long to execute.  The single loop would also eliminate the need for local variables.
 
Hope this helps!
Andy F. 
-----------------------------------------------------------------
National Instruments
0 Kudos
Message 2 of 4
(3,196 Views)
Thank you, Andy.

The reason I'm using 3 loops instead of 1 is that I'm still not very developed in my error-handling, so I'm not entirely sure how to deal with the various possible conditions (such as network disconnects and/or hardware powering down).

From what I've read, race conditions develop when a variable has 2 inputs writing into it. Will this also happen when an input is reading a variable while another is writing to it? The loops are all running 10 times a second, so whether the data is read/write before it is write/read doesn't really affect my programming. I will, however, try to fit everything into one loop without the use of variables if at all possible. Besides the uncertainty over data content, what other problems might arise from a race condition (between read and write, not between write and write)?

0 Kudos
Message 3 of 4
(3,193 Views)

Glad to help.  Our website defines race conditions as "[a] condition in digital electronics where two signals do not always arrive in the same order."  So if you are not concerned with your read/write order, then it isn't a huge deal.  However, most would recommend not using local variables unless otherwise necessary since it breaks the data flow programming scheme of LabVIEW.  I know I have ran into problems using them in an application that I thought was small, that became much bigger than I expected.  Following wires makes troubleshooting code much easier.  Smiley Happy  Anyway, hopefully I have been helpful. 

Andy F. 

-----------------------------------------------------------------
National Instruments
0 Kudos
Message 4 of 4
(3,171 Views)