Relay.Send failing with larger messages

I have an application running on a microcontroller using an XBEE 3 Cellular modem. It has been working well for years. In the past couple days we have been getting errors when trying to send packages from MQTT down the uart using relay.send. It works well for 99% of our messages but we have two larger ones (796 bytes and 1160 bytes) that fail 9 times out of ten with the error message ‘memory allocation failed, allocating 1144 bytes’, this is for the message that is 1160 bytes long.

I run gc.mem_free() to see whats available (usually around 21000 bytes), then i run gc.collect() and then i even put in a 5 second sleep to let it do its thing before sending but I still get that error.

relay.send(relay.SERIAL, ‘[NMSG] {}’.format(ack_bytes_to_send))

After trying to send I query available again and run a gc collect.
Is there anything else I should try to get these messages through? I am unclear what is causing this to pop up now, I even backed out my code and ran a version from a year ago which was having no issues and it is still occuring. I have FW ver 11720 Prod Bootloader: 232 Build: Nov 21 2024 14:21:51. Running micropython of course
Thanks for any help or pointers

Does your code use a buffer file to assemble your packet with? If so, you may need to make sure you are clearing it before going onto the next packet.

After pulling all recent code we traced it to the buetooth system. This started occuring once we added code to search for advertisements. We activate bluetooth and do a gap_scan then set bluetooth to inactive

ble.active(True)
try:
scanner = ble.gap_scan(30000, interval_us=50000, window_us=50000)
#other code to parse advertisements found
finally:
if scanner is not None:
scanner.stop()

ble.active(False)

With this code in play we fail sending a message over the serial relay every time when over 600bytes or so. If i take this code out we run fine. The bluetooth search runs every 10 minutes, is there anything else we can try to get past this or has it been seen before?
Thanks!

Commented put all the above sample code and it still occured. So I also commentedout the import of the ble library from digi, tht got rid of the error. Why would getting rid of

from digi import ble

stop the error and is there any way around this issue?

Possible bug in the BLE code? Not something I have ever experienced or heard of.

I do believe you are just running into memory fragmentation. I would statically allocate a message buffer that you re-use every time you need to compose a message and send it. After send is called on the relay message, you can wipe it and construct another (internally it gets copied into a separate buffer, separate from the MicroPython heap).
I do believe the Bluetooth observation is probably a red-herring as importing the Bluetooth package uses very little additional memory, but doing a scan will create a bunch of small dictionary objects that could worsen fragmentation.

P.S.
I looked and the max relay packet is 1500 bytes on XBee Cellular.

I would suggest looking at Digi MicroPython Programming Guide This indicates that your max packet you can send over BLE is 20 bytes.

Thanks for the idea, would you have an example of this you can point me to?

We dont, I grab a message from an mqtt callback funtion, then send the pertinent part using relay.send(relay.serial, message_to_send)
What would be the preferred way of doing this? Any section in the programming guide to look at? Thanks

You might need to load your mqtt call back into an array or do a read of 20 bytes first, then send. Then do another read of the next 20 bytes till there is nothing else left in the buffer.