LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do arrays and variables increase memory usage

I'm a little lost on arrays and memory usage. I was under the impression that an array was just so much memory. My processor is a NI8350 with 3Ghz speed and 2GB or RAM. That is more than enough speed and ram to run LV (8.2). In my program I have multiple while loops doing different things. Most just monitor stuff and do NOT generate data nor build arrays. Two loops though, the Monitor loop and the Active loop do use and gererate arrays and use lots of variables.
 
That being said, if I do not initialize an array, how can it possibly become larger than the data stored in it...thereby increasing memory usage and slowing things down....or does it? I have heard this about arrays too many times but don't really understand just HOW they impact memory usage and processor speed. If a variable is not initialized, just how large does the processor make it when first encountered? Is there a default size?
 
Same thing goes with local variables. OK so the pgm uses memory space for a variable. If I have few writes (or create variable) but a "LOT" of reads, why should the quantity of reads matter....or does it? If a variable is just a memory location....I would think that it would not matter how many reads you did.....you would just access the same location over and over. How does using variable slow down the processor.....especially when running the same program again and again? I find that every now and then I have to reboot the computer just to get the response time back to where it was. What is happening here?
 
Last question for now. I obviously need to go to a refresher course (intermediate) but are there any blurbs anywhere to explain my situations? Maybe some kind of "in depth" article on variables (and arrays) and memory usage.
0 Kudos
Message 1 of 5
(4,275 Views)
There have been a lot of discussions about memory allocation in the past, but briefly I touch on a few points. When an array is preallocated (initialized) to be a certain size LabVIEW is then able at compile time to set aside a certain amount of memory for that array. Then, if you are using the "replace array element" function within your loop the array isn't growing, just the values are being changed. If you are building the array in the loop, by appending to the existing array for instance, LabVIEW will continuously have to try and allocate memory space to the increasingly bigger array, sometimes having to move it to a whole new memory area. Additionally, and there are discussions of this in the other threads, if you branch the wires to different functions in the loop, LabVIEW will create copies of the initial array, compounding the whole problem, greatly effecting the execution speed, etc.  There have been a lot of discussions about the effects of using the various types of variables (e.g. locals and globals) and other efficiency impacts, for instance having an array of double connected to an indicator of single (or any other type mismatch) which is another thing that creates a copy of the data in memory. You can see that it wouldn't take too many of these to start having an impact on your programs speed. Do a search on this forum for memory usage, memory allocation, efficiency, etc. and you will see some of these discussions. One of the "nuggets" which initially deals with "Action Engines" discusses some of the causes and effects.  When you do search, check the date stamps, some of these might be a little old.


Message Edited by LV_Pro on 04-12-2007 02:24 PM

Message Edited by LV_Pro on 04-12-2007 02:26 PM

Putnam
Certified LabVIEW Developer

Senior Test Engineer North Shore Technology, Inc.
Currently using LV 2012-LabVIEW 2018, RT8.5


LabVIEW Champion



Message 2 of 5
(4,265 Views)
Tnx for info Putnam. I am now removing a lot of copies of variables I had previously used. That will help I am sure. I will also take a peek at my array usage relative to what you said. I did get lost on the "nuggets" part though. I assume you mean some kind of discussion in one of these forums. I will do a search on Action Engines also.....but would appreciate you pointing me in the right direction on what you actually meant by "nugget"
0 Kudos
Message 3 of 5
(4,251 Views)

Action Engine Nugget can be found here.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 4 of 5
(4,241 Views)

You already received some excellent answers, here are some more points (or similar points in other words).

Array resizings are expensive. Also remember that each indicator and control has its own data copy, so if you place a few array indicators along the dataflow of your code, you immediately generate multiple data copies. Local variables are always tied to front panel objects and thus inherently wasteful.

It is possible that you need to rethink your programming style. In LabVIEW, the wire is the variable so there are no "variables" needed in the classic sense of text based programs. A very efficient structure to hold array data is e.g. a shift register. If you have an array in a shift register and don't use any array resizing operations (delete from array, insert into array, built array), but exclusively use in-place operations (e.g. replace array subset), you are much better off.

Have a look at some of the older discussion threads, e.g. my rant from a long time ago:

http://forums.ni.com/ni/board/message?board.id=170&message.id=112401#M112401

Message 5 of 5
(4,214 Views)