bixente57
bixente57

Reputation: 1348

How to manage large strings

In a recent test of my ESP8266, I bumped into a memory limitation.

I have a simple http server that serves a json of the AP available around ESP8266.

function incoming_connection(conn, payload)
 conn:send('HTTP/1.1 200 OK\n')
 conn:send('Content-Type: application/json\n\n')
 conn:send(tableToJson1(currentAPs))
end

When I am at home, in Paris, AP list can be long, then I reach the payload max size.

In order to avoid this memory panic, I want to split my json in several chunks, then send one after the other.

I wrote this :

function splitString(str, maxlen)
  local t = {}
  for i=1, math.ceil(string.len(str)/maxlen) do
    t[i] = string.sub(str, (i-1)*maxlen+1, i*maxlen)
  end
  collectgarbage()
  return t
end

Then tried to test the function with this:

function genstr(len)
  local s=""
  for i=1,len do
    s=s.."x"
  end
  return s
end
for k,v in pairs(splitString(genstr(2000))) do print(v) end

Results of some test:

Length of generated string    +   Length of string chunks   +   Result
1500                          +   100                       +   OK
1500                          +   200                       +   OK
2000                          +   100                       +   Crashes after 8 lines
2000                          +   200                       +   Crashes after 4 lines

It seems that I reach a memory limit around 1500 bytes.

What would be your advice to go over this limit?

Upvotes: 3

Views: 1567

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

The problem is probably in genstr, not splitString.

Strings in Lua are immutable, in genstr, a new string is generated by each s=s.."x" in the loop.

  for i=1,len do
    s=s.."x"
  end

Instead, you could use the built-in string.rep (or, for more complicated cases, table.concat) to generate the test string.

Upvotes: 4

Related Questions