logo

Database

Improper Resource Allocation - Memory Leak

Need

To avoid exhausting system resources due to memory leaks

Context

• Usage of Elixir 1.12 for building scalable and concurrent applications

• Usage of Phoenix Framework 1.6 for web development

Description

1. Non compliant code

defmodule MemoryLeak do
  def leak_memory do
    spawn(fn ->
      Process.flag(:trap_exit, true)
      accumulate([])
    end)
  end
  defp accumulate(list) do...

The following code demonstrates a memory leak situation in Elixir. A process is spawned that starts a large data structure (a list), which is continually appended with elements. However, these elements are never released and accumulate over time, leading to increased memory usage.

2. Steps

• Monitor your processes and identify those that continue to consume memory over time

• Limit the lifetime of processes that create large data structures

• Consider making use of Elixir's garbage collection features to manually trigger garbage collection in certain situations

3. Secure code example

defmodule MemoryLeak do
  def leak_memory do
    spawn(fn ->
      Process.flag(:trap_exit, true)
      accumulate([])
      Process.sleep(10000)
      :ok
  end...

In this solution, the process is set to terminate after a certain period of time, ensuring that the large data structure it has created is released from memory.