sd_destroy_context: Release a stable diffusion context and free its VRAM

View source: R/pipeline.R

sd_destroy_contextR Documentation

Release a stable diffusion context and free its VRAM

Description

Immediately destroys an sd_ctx object created by sd_ctx, freeing the GPU memory held by its model weights and compute buffers. Use this before loading a different model so the two models do not pile up in VRAM.

Usage

sd_destroy_context(ctx)

Arguments

ctx

An sd_ctx object from sd_ctx.

Details

The context's external pointer also has a finalizer that frees it during R's garbage collection, but that is non-deterministic and may not run promptly — on a memory-constrained GPU, loading a second model before the first is collected can exhaust VRAM and make the next Vulkan device init fail. Calling sd_destroy_context() makes the release deterministic.

After this call the ctx object is dead; do not pass it to sd_generate or other functions. Calling it twice on the same object, or on an already-finalized one, is a safe no-op.

Value

NULL, invisibly.

See Also

sd_ctx

Examples

## Not run: 
ctx <- sd_ctx("flux1.safetensors", model_type = "flux")
img <- sd_generate(ctx, "a cat")
sd_destroy_context(ctx)              # free VRAM before the next model
ctx <- sd_ctx("flux2.safetensors", model_type = "flux2")

## End(Not run)

sd2R documentation built on June 19, 2026, 9:08 a.m.