Architectural Invariant Design: Deterministic Skiko Native Resource Disposal in Compose Multiplatform
Deterministic Skiko Native Resource Disposal in Compose Multiplatform defines an industrial-grade behavioral standard for mobile engineering. Enforces immediate and deterministic deallocation of native Skia/Skiko resources (such as Image, Surface, PictureRecorder, and NativePointer wrappers) across Compose Multiplatform iOS and Desktop targets. Mitigates native off-heap memory exhaustion and iOS Jetsam kills caused by Kotlin/Native GC latency when handling high-frequency view recycling. Guarantees that canvas raster buffers and transient graphics contexts are closed explicitly via lifecycle hooks rather than deferred finalizers. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
Hazard Analysis: What Breaks Without This Rule
The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:
| Failure Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Unbounded Native Buffer Accumulation via GC-Deferred Finalization in Skiko Rendering | Critical | Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production. Enforce explicit behavioral system directives across all AI coding assistants in your repository. |
| Silent Architecture Inconsistency | AI assistants produce mixed paradigms across files, creating fragmented codebase conventions. | Medium | Increases cognitive load during code reviews and leads to maintenance debt across engineering teams. Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS invoke `.close()` or `.use { ... }` on Skiko Managed objects (e.g., `org.jetbrains.skia.Image`, `org.jetbrains.skia.Surface`, `org.jetbrains.skia.PictureRecorder`) as soon as their lifecycle ends.
Engineers MUST ALWAYS invoke `.close()` or `.use { ... }` on Skiko Managed objects (e.g., `org.jetbrains.skia.Image`, `org.jetbrains.skia.Surface`, `org.jetbrains.skia.PictureRecorder`) as soon as their lifecycle ends.
Engineers MUST NOT allocate native Skia/Skiko buffers or decode raster data inside Compose draw phases (`Modifier.drawWithContent`, `Canvas`, or `DrawScope`) without pooling or retaining references in a lifecycle-bound state holder.
Engineers MUST NOT allocate native Skia/Skiko buffers or decode raster data inside Compose draw phases (`Modifier.drawWithContent`, `Canvas`, or `DrawScope`) without pooling or retaining references in a lifecycle-bound state holder.
Code SHOULD bind long-lived native Skia references to the composition lifecycle using `DisposableEffect`, ensuring that `onDispose` explicitly triggers `.close()`.
Code SHOULD bind long-lived native Skia references to the composition lifecycle using `DisposableEffect`, ensuring that `onDispose` explicitly triggers `.close()`.
Services MUST implement pooling mechanisms with bounded native off-heap memory quotas for reusable bitmaps and surfaces when rendering high-frequency collections (`LazyColumn`, `LazyRow`).
Services MUST implement pooling mechanisms with bounded native off-heap memory quotas for reusable bitmaps and surfaces when rendering high-frequency collections (`LazyColumn`, `LazyRow`).
Components NEVER rely on Kotlin/Native Garbage Collection or finalize routines to reclaim off-heap C++/Metal textures, as the GC scheduler is unaware of external native memory pressure.
Components NEVER rely on Kotlin/Native Garbage Collection or finalize routines to reclaim off-heap C++/Metal textures, as the GC scheduler is unaware of external native memory pressure.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Deterministic Skiko Native Resource Disposal in Compose Multiplatform critical for AI coding assistants?
Enforces immediate and deterministic deallocation of native Skia/Skiko resources (such as Image, Surface, PictureRecorder, and NativePointer wrappers) across Compose Multiplatform iOS and Desktop targets. Mitigates native off-heap memory exhaustion and iOS Jetsam kills caused by Kotlin/Native GC latency when handling high-frequency view recycling. Guarantees that canvas raster buffers and transient graphics contexts are closed explicitly via lifecycle hooks rather than deferred finalizers. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.
Q:How does adopting this rule prevent AI hallucinations?
This rule explicitly declares what the AI must avoid (Unbounded Native Buffer Accumulation via GC-Deferred Finalization in Skiko Rendering) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.
Q:Which AI coding environments are supported?
This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).
Q:Can I combine this rule with other technology stacks?
Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.