B4R Question Memory usage on Uno

janderkan

Well-Known Member
Licensed User
Longtime User
If I add a function call I can see the the sketch uses more storage space.
If I increase the size of an array variable I can see the sketch uses more dynamic memory.

If I use #StackBufferSize: 150
This is the result when i compile my program:

Sketch uses 30724 bytes (95%) of program storage space. Maximum is 32256 bytes.
Global variables use 1261 bytes (61%) of dynamic memory, leaving 787 bytes for local variables. Maximum is 2048 bytes.
Everything works fine.

If I increase the #StackBufferSize: 300
The sketch still uses 30724 bytes and Variables still use 1261 bytes.
But now the program stops at random.

If I lower the size of an array I can increase the Stackbuffersize and the program still works.

Where is the Stackbuffer allocated ?
Why does the program stop when Stackbuffersize is incremented with 150 bytes, it looks like there are room for more ?
How can I calculate the correct size of used memory so the program will work forever ?

Jan
 

janderkan

Well-Known Member
Licensed User
Longtime User
Here is a little info for people that are low on memory.

I like to divide my program into logical modules.
F.ex. The Can-Bus code in 1 module and other logic in another.

I used this code to send 40 packets from logic module:
B4X:
modCan.SendPacket(array as byte(B0,b1))


Then I tried to add a Sub to send the packets:
B4X:
private Sub SendCanMsg(Data() As Byte)
    modCAN.SendMessage(Data)
End Sub
and changed the 40 calls to this:
B4X:
SendCanMsg(array as byte(109,StateVal))
It did not change anything!


Then I changed the sub to this:
B4X:
private Sub SendCanMsg2(Packet As Byte,B1 As Byte)
    modCAN.SendMessage(Array As Byte(Packet,B1))
End Sub
and I changed the 40 calls to this:
B4X:
SendCanMsg2(109,StateVal)
This saved me 1510 bytes of memory (4.6% on a UNO) and the program is still doing the same thing !!

If anyone have other tips to save memory, I would be glad to know how.

Jan
 
Upvote 0
Top