Skip to content

Memory Leak in GarbageCollector due to uncleared to_remove map #855

@themiguelamador

Description

@themiguelamador

Bug Description

A memory leak has been identified in the live_debugger library, specifically in the GarbageCollector GenServer. The leak is caused by the to_remove map in the GarbageCollector's state, which grows indefinitely without being cleaned up.

The GarbageCollector is responsible for periodically cleaning up ETS tables and other resources used by the debugger. It uses a to_remove map to keep track of the processes that are marked for deletion. When a process is garbage collected, its pid is supposed to be removed from the to_remove map. However, this is not happening.

The bug is located in the handle_info(:garbage_collect, ...) function in lib/live_debugger/services/garbage_collector/gen_servers/garbage_collector.ex. This function recalculates the to_remove map in each cycle, but it never cleans up the old entries.

As a result, the to_remove map grows indefinitely, causing a memory leak. This is especially noticeable in development environments where the live_debugger is active and many LiveViews are being created and destroyed.

Actual Behavior

When the live_debugger is enabled in a Phoenix LiveView application, the memory usage of the BEAM process grows over time and never decreases, even with low activity. This eventually leads to the application crashing due to an out-of-memory error.

Expected Behavior

The memory usage of the BEAM process should remain stable over time, and it should not grow indefinitely when the live_debugger is enabled.

Steps to Reproduce

  1. Enable the live_debugger in a Phoenix LiveView application.
  2. Navigate through the application, creating and destroying LiveViews.
  3. Monitor the memory usage of the BEAM process.
  4. Observe that the memory usage grows over time and never decreases.

Elixir version with Erlang/OTP

Erlang/OTP 28 [erts-16.1.1] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [jit] [dtrace] Elixir 1.19.3 (compiled with Erlang/OTP 28)

LiveDebugger version

0.4.3

Phoenix version

1.8.1

Phoenix LiveView version

1.1.17

Operating system

darwin

Browser(s)

Brave

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions