Skip to content

M0202: Allocation in an arena loop ​

warning

What it means ​

An allocation inside a loop uses a Scoped arena. Nothing is freed until the arena closes, so memory grows with every iteration. This is a warning: the program is still accepted (-Werror turns it into a failure).

Minimal failing example ​

From the test corpus: compiler/testdata/memory/arena_loop_warn.kt. The corpus expects 15:25: M0202 (the ^ comment in the source marks the column).

kotlin
// ACCEPT with warning M0202: allocation in a loop inside one arena grows without bound. Spec 8, 7.18
@file:MemoryMode(Mode.Strict)

package memory.arena_loop_warn

import mini.memory.*

class Box(val v: Int)

fun main() {
    var total = 0
    Arena().use { arena ->
        context(arena) {
            for (i in 0 until 3) {
                val b = Box(i)
//                      ^ M0202
                total += b.v
            }
        }
    }
    println(total)
}

Compiler output ​

Generated by running ./minic check compiler/testdata/memory/arena_loop_warn.kt (exit status 0: a warning does not fail the build).

text
$ ./minic check compiler/testdata/memory/arena_loop_warn.kt
warning[M0202]: allocation in a loop inside arena `arena`: unbounded growth
  --> compiler/testdata/memory/arena_loop_warn.kt:15:25   allocates in every iteration
   15 |                 val b = Box(i)
      |                         ^^^
  help: hoist the allocation out of the loop or reset/recreate the arena per iteration

note: M0202 (Allocation in an arena loop): https://henryschwerdt.github.io/mini/reference/diagnostics/M0202

warning: arena_loop_warn.kt generated 1 warning

How to fix ​

Hoist the allocation out of the loop, or reset/recreate the arena per iteration.

Corpus programs for this code ​

See also: Diagnostics overview and the memory guide.

Released under the Apache License 2.0.