Skip to content
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

heap fragmentation #39

Open
SolarDaniel opened this issue Apr 7, 2024 · 0 comments
Open

heap fragmentation #39

SolarDaniel opened this issue Apr 7, 2024 · 0 comments

Comments

@SolarDaniel
Copy link
Contributor

In some services Strings are used as members.
They are initialized with empty buffer.
As an example WifiSettingsService.h.

// Struct defining the wifi settings in WifiSettingsService.h
typedef struct
{
    String ssid;
    String password;
    bool staticIPConfig;
    IPAddress localIP;
    IPAddress gatewayIP;
    IPAddress subnetMask;
    IPAddress dnsIP1;
    IPAddress dnsIP2;
    bool available;
} wifi_settings_t;

When changing the settings from the GUI, a JSON document is dynamically allocated and the update() is called, where the Strings are set. The String class then allocates memory and copies the content to the memory from the heap, where it lives until it's changed again. But the JSON document (sometimes 2000 to 4000 bytes) is freed after the update.
A gap is left on the heap. It will be used later on, but the heap gets fragmented and the maximum allocatable heap shrinks.

The same happens when using persistancy, when initial data is loaded from FS..

My proposal is to use String.reserve() in the constructor for these members.
The size of the content is known in advance and can be well limited.
So the framework would leave the maximum heap possible to the user and not fragment the heap already at startup.

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

No branches or pull requests

1 participant