---
title: "Measure GPU buffer memory usage in C#"
url: https://recursiveai.org/onegpu/cookbook/measure-gpu-memory-allocations/
description: "Measure GPU buffer memory in C# with OneGpu on Vulkan. Compare used and reserved bytes before allocating buffers, while they’re live, and after disposal."
version: "1.0.0"
---

# Measure GPU buffer memory usage in C#

**OneGpu** is a free, open-source GPU compute library for .NET from Recursive AI. MIT or Apache-2.0, no bundled native binaries.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGpu.Backends) · [GitHub](https://github.com/recursive-ai-oss/onegpu-csharp) · [Docs](https://recursiveai.org/onegpu/docs/) · [Download example (.zip)](https://recursiveai.org/onegpu/cookbook/measure-gpu-memory-allocations/measure-gpu-memory-allocations.zip)

Read used bytes before, during, and after a buffer allocation. This measures OneGpu’s allocator, not total VRAM use for your process or GPU. Requires Vulkan 1.3 on Windows or Linux.

```shell
dotnet add package RecursiveAI.OneGpu.Backends
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using System;
using RecursiveAI.OneGpu.Vulkan;

using var gpu = VulkanGpu.CreateDevice();
PrintUsed();
using (gpu.CreateBuffer<float>(256 * 1024))
    PrintUsed();
gpu.Submit(gpu.BeginCommands());
gpu.WaitIdle();
PrintUsed();

void PrintUsed() =>
    Console.WriteLine(gpu.MemoryStats.UsedBytes);
```

Unzip [the example](https://recursiveai.org/onegpu/cookbook/measure-gpu-memory-allocations/measure-gpu-memory-allocations.zip), then `dotnet run`.

Prints used bytes on an NVIDIA GeForce RTX 4070:

```text
0
1048576
0
```

## The full program

This version allocates two buffers and compares `UsedBytes`, `ReservedBytes` and `Allocations`. It also reads the counters just after disposal, before the next submission gives Vulkan a chance to collect the retired buffers. If the allocation can’t be satisfied, `GpuOutOfMemoryException.RequestedBytes` reports the requested size.

```csharp
using System;
using RecursiveAI.OneGpu;
using RecursiveAI.OneGpu.Vulkan;

using var gpu = VulkanGpu.CreateDevice(
    new() { Adapter = "discrete" });
Console.WriteLine(gpu.Adapter.Name);
Show("Before", gpu.MemoryStats);
using (gpu.CreateBuffer<float>(256 * 1024))
using (gpu.CreateBuffer<float>(256 * 1024))
    Show("Live", gpu.MemoryStats);
Show("Just disposed", gpu.MemoryStats);
// Beginning the next submission lets Vulkan collect retired buffers.
gpu.Submit(gpu.BeginCommands());
gpu.WaitIdle();
Show("Collected", gpu.MemoryStats);

static void Show(string label, GpuMemoryStats stats)
{
    Console.WriteLine($"{label}: {stats.UsedBytes} B used, " +
        $"{stats.ReservedBytes} B reserved, " +
        $"{stats.Allocations} allocations");
}
```

Output on Linux with Vulkan:

```text
NVIDIA GeForce RTX 4070
Before: 0 B used, 0 B reserved, 0 allocations
Live: 2097152 B used, 67108864 B reserved, 2 allocations
Just disposed: 2097152 B used, 67108864 B reserved, 2 allocations
Collected: 0 B used, 67108864 B reserved, 0 allocations
```

The 64 MiB stays reserved because OneGpu keeps the driver block for reuse after both buffers are freed.

## Variations

### Isolate upload staging memory

Print this immediately after the `Collected` snapshot. The example allocates buffers without uploading any data, so this run has no upload staging memory:

```csharp
Console.WriteLine($"Staging: {gpu.MemoryStats.StagingBytes} B");
```

```text
Staging: 0 B
```

## Related recipes

- [Choose a Vulkan physical device](https://recursiveai.org/onegpu/cookbook/pick-a-vulkan-device/)
- [Read a GPU buffer back to the CPU](https://recursiveai.org/onegpu/cookbook/read-back-a-gpu-buffer/)
- [Time a GPU compute pass](https://recursiveai.org/onegpu/cookbook/time-a-compute-pass/)

## The short answer

Read `gpu.MemoryStats.UsedBytes` immediately before and during the phase you’re measuring. Subtract the first value from the second to get the change in bytes occupied by resources that OneGpu tracks for that device. Read again after disposal and collection to see what remains. This measures changes in OneGpu’s allocator, not total VRAM usage for your process or GPU.

## What is the difference between used and reserved GPU memory?

On Vulkan, OneGpu reserves blocks from the driver and allocates resources from those blocks. `ReservedBytes` includes space in those blocks that isn’t occupied by an allocation. `UsedBytes` counts occupied bytes, including alignment padding, and `Allocations` counts live resource allocations, not driver blocks. In the full run, the two `float` buffers occupy 2 MiB between them, while the allocator holds a 64 MiB block. Those are observations on one driver and adapter, not sizes to hard-code in your own memory estimates.

Upload memory is a separate detail. On Vulkan, `StagingBytes` reports the host-visible upload staging that the backend holds; it’s a subset of `ReservedBytes`, not a number to add on top. An application that uploads to a buffer can therefore see a different reserved total from this example even when the buffers have the same dimensions. See the [memory statistics documentation](https://recursiveai.org/onegpu/docs/compute/#memory-statistics) for which counters the backends report; WebGPU reports zeros, not an estimate.

## Why does GPU memory stay allocated after a buffer is freed?

Disposing a buffer ends your use of it, but work already submitted to the GPU may still reference it. The Vulkan backend defers reclamation and checks for completed work when beginning a command buffer. In the full program, the immediate snapshot still counts the two retired allocations. The empty submission and wait are there to make the final snapshot in this small example, not something you need to add after every buffer in a normal compute loop.

Even after the allocations stop counting as used, the allocator can retain the driver block. Watch the used and reserved counters together: if `UsedBytes` and `Allocations` return to baseline but `ReservedBytes` does not, that snapshot does not say that the disposed buffers are still live. It says that the allocator still holds memory it can use for subsequent allocations.
