Hi there,
I'm encountering an error when trying to run code in the Lua script debugger in Kipling (Mac) on our T7-Pro. I've been getting the error "lua: not enough memory" (screenshot attached) on-and-off for a little while now, although at the moment it seems to be every time I try to run code.
For starters, not enough memory for what? My program is <300 lines, so I wouldn't have thought program memory was the issue.
I'm particularly suspicious because the last few times I got this error I simply disconnected the T7, reconnected it, and voila the code ran.
Does anybody have experience in the area as to what might be happening?
Many thanks :)
The T7 only has 64 kBytes of heap. That memory is used for Lua as well as buffers etc. Features like AIN_EF, serial comm and stream also need memory from the heap. When you load a lua script the source is saved to the heap and compiled to lua byte-code. There has to be enough memory to store both the byte-code and the source. At that point the source is deleted and the lua virtual machine can be started. Now there needs to be enough memory for the byte-code, the VM and variables used by your program. The number of lines of code is only one of the memory related constraints.
Things are rather tight in the 64 kBytes available. Do you get memory errors when lua is compiling or when running? If you get errors when compiling you can reduce RAM requirements by shortening variable names and shortening or removing comments. If you are getting errors when lua is running we need to reduce RAM usage by being more efficient with variables or simplifying code.
As lua runs it will place variable on the heap and occasionally remove when they are no longer needed. This can result in fragmented memory. You might need 128 bytes and could have 256 byte available in individual 16 Byte chunks that are not next to each other. That will cause an error because the memory needs to be contiguous for programs to operate correctly. You can combat this by instructing lua to collect garbage variable at regular intervals: http://www.lua.org/manual/5.1/manual.html#pdf-collectgarbage
Here is a nice explanation of the collectgarbage function: http://luatut.com/collectgarbage.html
The above techniques may get your existing program to run reliably, but you are likely near the limit of what you will be able to add to your program.
I believe it's happening at compile (although the debug window doesn't give much away). So I suspect, as you say, we are at our program limit. This certainly crept up sooner than anticipated.
Thanks for your assistance.
Dear LabJack users, dear LabJack developers,
I am using T7-pro (with updated firmware) for temperature control via PID. I am testing my Lua Script via Kipling, but my final goal is to use the script embedded.
During the first tests in Kipling, I got the error "not enough memory" when the code is running. I performed some memory profiling via collectgarbage() and I discovered that:
1. If collectgarbage() is called in the first line of the code, the memory is about 17 KB.
2. At about 45 KB of used memory, I get the error "not enough memory".
I really need to understand what's going on in order to use the device. Therefore, I have the following questions:
1. Why does every Lua script (also empty script) have 17 KB used at the very first line? Is this because of the libraries linked to the script?
2. Why do I have memory issues at 45 KB and not 64 KB? Is it caused maybe by the Lua VM (and therefore, VM should be around 18 KB, since 45 + 18 + 1 = 64)?
Thank you very much in advance
J
JV,
At this time we don't have any way to determine if the T7 will report this error when trying to start a script before trying to run it and it is hard to speak specifically to your numbers. The Lua VM does have a start-up penalty so your intuition regarding question #1 is most likely correct. For #2, this number really depends on what features of the device you are using at any given point in time. The Lua VM takes RAM, and many of the T7's other features also consume RAM so this is not a trivial number to calculate and is highly dependent on your specific application.
One thing you can do before running your script to get the largest script possible onto the T7 is to run it through a Lua minifier like this one available online:
https://mothereff.in/lua-minifier