Skip to content

Fix run out of heap on ESP32 #6819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Conversation

nikito7
Copy link

@nikito7 nikito7 commented May 15, 2025

🤝 Attestations

  • I have tested that my proposed changes behave as described.
  • I have tested that my proposed changes do not cause any obvious regressions on the following devices:
    • Heltec (Lora32) V3
    • LilyGo T-Deck
    • LilyGo T-Beam
    • RAK WisBlock 4631
    • Seeed Studio T-1000E tracker card
    • Other (please specify below)

ESP32 devices.

@CLAassistant
Copy link

CLAassistant commented May 15, 2025

CLA assistant check
All committers have signed the CLA.

@nikito7 nikito7 marked this pull request as ready for review May 15, 2025 11:09
@fifieldt
Copy link
Member

Hi @nikito7 , thanks for your patch :)

Would you be able to tell us more about the background? What situation did you run out of heap? Also, is there any impact on other uses of the memory by making this change?

@nikito7
Copy link
Author

nikito7 commented May 16, 2025

It may only be used here:

// returns true if the maximum number of nodes is reached or we are running low on memory
bool NodeDB::isFull()
{
    return (numMeshNodes >= MAX_NUM_NODES) || (memGet.getFreeHeap() < MINIMUM_SAFE_FREE_HEAP);
}

return (numMeshNodes >= MAX_NUM_NODES) || (memGet.getFreeHeap() < MINIMUM_SAFE_FREE_HEAP);

If 1500 default it's all free heap, it's bad.
ESP32 below 15kb could crash.

This it's only for nodes. I don't know if impact anywhere.

Because my true solution for my problem was:

  -D MESHTASTIC_EXCLUDE_BLUETOOTH=1
  -D MESHTASTIC_EXCLUDE_AUDIO=1
  -D MESHTASTIC_EXCLUDE_PAXCOUNTER=1
  -D MESHTASTIC_EXCLUDE_RANGETEST=1
  -D MESHTASTIC_EXCLUDE_ATAK=1
  -D MINIMUM_SAFE_FREE_HEAP=25000

My current it's about 90kB now.
With default firmware it's always below 20kB.
And wifi hangs or esp32 reboot.

Screenshot_20250515_083505_Home Assistant

@thebentern thebentern requested a review from caveman99 May 16, 2025 11:17
@nikito7
Copy link
Author

nikito7 commented May 16, 2025

Main problem it's Bluetooth.
But this 'MINIMUM_SAFE_FREE_HEAP' should the used more in code.

I only found it in Nodes.

Heap fragmentation could be a issue too.


Screenshot_20250516_162511_Home Assistant

After update to BLE version:

  • ping OK
  • RF OK
  • API down
  • Atfer 10min, API ok

@mverch67
Copy link
Collaborator

mverch67 commented May 17, 2025

I don't think the proposed solution is reasonable. Instead of changing the global value (which is also used for e.g. nrf devices that do not have much memory) you can limit the use to specific devices in the platformio.ini files (see also arch folder).
Also, this value has been used for months or even years without any major problems and now you suggest the ten-fold value which seems quite exaggerated. Did you try other values like 2000 or 2500, 3000, and so on?

@nikito7 nikito7 marked this pull request as draft May 17, 2025 13:21
@nikito7 nikito7 changed the title Fix run out of heap Fix run out of heap on ESP32 May 20, 2025
@nikito7 nikito7 marked this pull request as ready for review May 20, 2025 07:13
@nikito7
Copy link
Author

nikito7 commented May 20, 2025

This PR it's mainly a proof of concept.
If you don't agree, it's ok.
I don't have the perfect solution.
The main problem it's Bluetooth itself.

If someone have issues, adapt this for your needs:

[han32opt]
build_flags =
  -D PRIVATE_HW
  -D DEFAULT_REBOOT_SECONDS=3
  -D MESHTASTIC_EXCLUDE_PAXCOUNTER=1
  -D MESHTASTIC_EXCLUDE_RANGETEST=1
  -D MESHTASTIC_EXCLUDE_ATAK=1
  -D MESHTASTIC_EXCLUDE_STOREFORWARD=1

[env:han32gw-no-ble]
extends = esp32_base
board = esp32dev
board_check = true
build_flags = 
  ${esp32_base.build_flags}
  ${han32opt.build_flags}
  -I ../variants/han32gw
  -D MESHTASTIC_EXCLUDE_BLUETOOTH=1
  -D MINIMUM_SAFE_FREE_HEAP=25000
  -D MAX_NUM_NODES=100
upload_speed = 460800

[env:han32gw-ble]
extends = esp32_base
board = esp32dev
board_check = true
build_flags = 
  ${esp32_base.build_flags}
  ${han32opt.build_flags}
  -I ../variants/han32gw
  -D MESHTASTIC_EXCLUDE_BLUETOOTH=0
  -D HAS_BLUETOOTH=1
  -D MINIMUM_SAFE_FREE_HEAP=15000
  -D MAX_NUM_NODES=100
upload_speed = 460800

; EOF

@nikito7
Copy link
Author

nikito7 commented May 20, 2025

A last comment. My ble firmware stills hangs wifi connection after a while. I need to reboot Acess Point.

Without BLE stack it's perfect.

Screenshot_20250520_135307_Chrome

@nikito7 nikito7 closed this May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants