janderkan Well-Known Member Licensed User Longtime User Apr 17, 2021 #1 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
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
Erel B4X founder Staff member Licensed User Longtime User Apr 18, 2021 #2 The stack buffer size is allocated on the heap at runtime, so it will not appear in the compilation output. There are two keywords that can help you with managing the limited available memory: AvailableRam and StackBufferUsage Upvote 0
The stack buffer size is allocated on the heap at runtime, so it will not appear in the compilation output. There are two keywords that can help you with managing the limited available memory: AvailableRam and StackBufferUsage
janderkan Well-Known Member Licensed User Longtime User Apr 19, 2021 #3 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
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
Erel B4X founder Staff member Licensed User Longtime User Apr 20, 2021 #4 janderkan said: his saved me 1510 bytes of memory Click to expand... (You are referring to storage space memory.) Upvote 0
janderkan said: his saved me 1510 bytes of memory Click to expand... (You are referring to storage space memory.)