Skip to content

Large Lua string allocation/concatenations crashes Lua environment. #7

@tlubke

Description

@tlubke

Doing iterative string building like this can lead to issues.

local n = 9

for i=1,n do
  local s = ""
  for i=1,(2^i) do
    s = s.."!"
  end
end

For n = 9, this is fine, but n = 10 appears to hang something in the firmware. LEDs will hold their state, diii continues to show as connected, but all code execution seems to stop. Soft reboot via ^^r doesn't work, but physically reconnecting seems to fix things.

Interesting to note that building one constant string of size 2^10 does not cause the same behavior. Maybe the strings spawned by concatenation are not garbage collected fast enough and cause an out-of-memory failure because of it?

EDIT: Quick calc using the summation formula would mean n=9 => 131,238B and n=10 => 524,800B assuming nothing gets free'd before the end of the loop. n=10 would be more than available SRAM of the Pico 2.

EDIT 2: Yeah, I think garbage collection is the issue. Adding collectgarbage("collect") to the inner for-loop fixes the crash. I don't think this allocation/collection problem is realistically something to fix, but would be nice to keep the Lua environment running if possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions