Stack size/heap size and System.freeMemory()

Hi all,

I’m working on a project that requires a secure posting of data from a photon. I’m using the tls embed library found here:

I have been working closely from the example provided in library. By the nature of the project, I have to send a very large chunk of data(roughly 10kb). I would like to avoid splitting it up into multiple post requests if possible. Anyways, when I try and allocate a buffer statically, I am only allow to allocate 500bytes before I get the SOS light. But when I allocate the buffer dynamically, I’m allowed up to 4kb before this happens(note that whatever the library does with the certificates takes up a lot of room). Is the stack size that much smaller on the photon than the heap? Also, even after I allocate the 4kb buffer dynamically, a call to System.freeMemory() shows that I have roughly 5kb of free memory left. What is the deal with this? Does the System.freeMemory() call give an accurate amount of memory that’s available?

@Newcomer118, there is no simple answer here. System.freeMemory() may be reporting overall heap but not its fragmentation. So when you try and allocate 10Kb, it may not be contiguous and won’t allocate. I suggest you allocate your buffer (globally to avoid allocation/deallocation fragmentation) before the TLS object does. If that can’t be done (because it’s done in the constructor), then allocate before TLS does anything else which might cause fragmentation. You may need to experiment to get it working.

1 Like