Skip to content

Fix localtime/1 mem leak#2083

Draft
petermm wants to merge 4 commits intoatomvm:release-0.7from
petermm:fix-localtime-leak
Draft

Fix localtime/1 mem leak#2083
petermm wants to merge 4 commits intoatomvm:release-0.7from
petermm:fix-localtime-leak

Conversation

@petermm
Copy link
Copy Markdown
Contributor

@petermm petermm commented Feb 3, 2026

Fixes #2062

replicate:

  def myloop do
    :erlang.localtime("CET-1CEST,M3.5.0,M10.5.0/3")
    |> IO.inspect()

    :timer.sleep(100)

    :erlang.system_info(:esp32_free_heap_size)
    |> IO.inspect()

    myloop()
  end

And see rapidly decreasing free memory - solution inspired by espressif/esp-idf#9764

Currently in draft, as I believe there is some issue with setting the tz to "" on generic_unix - which means it is set to utc, just need to understand that or guard it..

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@petermm petermm force-pushed the fix-localtime-leak branch from e336026 to 3683b3a Compare February 3, 2026 20:47
@bettio
Copy link
Copy Markdown
Collaborator

bettio commented Feb 6, 2026

This is a non trivial issue and I'm investigating a fix as well.

@petermm petermm force-pushed the fix-localtime-leak branch from 3683b3a to d791eaf Compare April 5, 2026 19:41
@petermm petermm changed the base branch from main to release-0.7 April 5, 2026 19:41
petermm added 3 commits April 5, 2026 21:42
espressif/esp-idf#9764

Signed-off-by: Peter M <petermm@gmail.com>
Guard the static putenv buffer workaround behind __NEWLIB__/__PICOLIBC__
so platforms with non-leaking libc (glibc, musl) use standard
setenv/unsetenv. Picolibc (used by ESP-IDF 6) has the same setenv leak.

Additional fixes:
- Replace space-padding with zero-fill to avoid TZ parser issues
- Reject oversized TZ values instead of silently truncating
- Restore "UTC0" instead of empty string when TZ was originally unset
- Check strdup for OOM, putenv return value, and localtime_r for NULL

Signed-off-by: Peter M <petermm@gmail.com>
@petermm petermm force-pushed the fix-localtime-leak branch from d791eaf to d3169f4 Compare April 5, 2026 19:43
Signed-off-by: Peter M <petermm@gmail.com>
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.

Memory leak in erlang:localtime/1

2 participants