# Debugging memory leaks

**URL:** <https://openrndr.discourse.group/t/debugging-memory-leaks/762>\
**Category:** Tutorials\
**Created:** [December 1, 2025, 3:17pm UTC](https://openrndr.discourse.group/t/debugging-memory-leaks/762 "2025-12-01T15:17:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![abe](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/abe/32/700_2.png) [@abe](https://openrndr.discourse.group/u/abe)\
**Post date:** [December 1, 2025, 3:17pm UTC](https://openrndr.discourse.group/t/debugging-memory-leaks/762/1 "2025-12-01T15:17:12Z")

</div>

Probably the most frequent reason for a memory leak is creating endless `ColorBuffer` instances without calling `.destroy()` on them. But it’s not the only one.

I wrote a short function that could help diagnose which resource is growing out of control:

```kotlin
        fun showSessionCounts(session: Session.Companion) {
            fun count(f: Session.() -> Set<*>) = session.stack.sumOf { f(it).size }
            writer {
                box = drawer.bounds.offsetEdges(-50.0)
                text(
                    listOf(
                        "colorBuffers: ${count { colorBuffers }}",
                        "arrayCubemaps: ${count { arrayCubemaps }}",
                        "arrayTextures: ${count { arrayTextures }}",
                        "atomicCounterBuffers: ${count { atomicCounterBuffers }}",
                        "bufferTextures: ${count { bufferTextures }}",
                        "computeShaders: ${count { computeShaders }}",
                        "cubemaps: ${count { cubemaps }}",
                        "depthBuffers: ${count { depthBuffers }}",
                        "indexBuffers: ${count { indexBuffers }}",
                        "renderTargets: ${count { renderTargets }}",
                        "shaderStorageBuffers: ${count { shaderStorageBuffers }}",
                        "shaders: ${count { shaders }}",
                        "vertexBuffers: ${count { vertexBuffers }}",
                        "volumeTextures: ${count { volumeTextures }}",
                    )
                )
            }
        }

```

You can place this function inside your `Program` block, then call it inside `extend { }` with `showSessionCounts(Session)`.

It will display some text like this:

 ![2025-12-01_15:25:14](https://global.discourse-cdn.com/free1/uploads/openrndr/original/1X/4882d9b0682cb911cb8d9c030215c6ba28f12b9d.png)

None of those number should grow out of control. If they do, you have now an idea on what to investigate 😉 .

---

<div class="post-metadata">

**Author:** ![abe](https://yyz2.discourse-cdn.com/free1/user_avatar/openrndr.discourse.group/abe/32/700_2.png) [@abe](https://openrndr.discourse.group/u/abe)\
**Post date:** [December 2, 2025, 4:14pm UTC](https://openrndr.discourse.group/t/debugging-memory-leaks/762/2 "2025-12-02T16:14:44Z")

</div>

Thanks to [this commit](https://github.com/openrndr/openrndr/commit/b4e01a5cc4a7a6651b092ae462e31012184a3e3b), in (future) `openrndr` versions greater than `v0.5.0-alpha2` this will be simpler:

```kotlin
// Log the resource stats to the console
println(Session.statistics)

```

or

```kotlin
// Render the resource stats as an overlay, like in the image above
writer {
    box = drawer.bounds.offsetEdges(-50.0)
    text(Session.statistics.toString().substringAfter('(').dropLast(1).split(", "))
}

```
