Skip to content
Limited timeStarter is now $9 a month instead of $20.See plans
charmotion

How to Prevent Rive Animation Memory Leaks & High GPU Usage in Web and Mobile Apps

By the Charmotion team · Updated · How we write guides

Cover illustration for the guide: How to Prevent Rive Animation Memory Leaks & High GPU Usage in Web and Mobile Apps, with Charmotion demo characters

Prevent Rive memory leaks and GPU spikes by creating one Rive instance per visible animation, tearing it down when its component unmounts, pausing or unmounting animations that are off screen, and choosing the lightest renderer your screen needs. Most leaks come from instances created on every render or never cleaned up.

Key takeaways

  • Most Rive leaks come from app code that creates instances repeatedly or never releases them, not from the .riv file.
  • For most React pages, use @rive-app/react-canvas and only move to another package when a page specifically needs it.
  • Guard every state machine input read, because the hook returns null until the Rive object and input exist.
  • Pause playback for off-screen and hidden content, and release native views and controllers on every platform when the screen closes.
  • Confirm the fix with a before and after memory profile that navigates away and back several times.

What causes Rive memory leaks and high GPU use in React?

This guide shows how to prevent Rive animation memory leaks in React and mobile apps, starting with the causes and then the fix for each platform. Most of the trouble comes from the app code around the runtime rather than from the .riv file itself.

  • Instances created on every render. A component that builds a new Rive instance each time it re-renders can leave the old ones running.
  • Instances that outlive their screen. A mascot on a page the user has left keeps drawing unless something stops it.
  • Too many instances on one page. A feed with 30 animated avatars gives the GPU 30 canvases to update. Use one live instance for the visible item and a static image for the rest.
  • Playback that nobody can see. Animations off screen or in a hidden tab still cost frames.
  • Heavy files loaded again and again. Each load of a large .riv and its WebAssembly adds memory until the old copy is released.

Rive's runtimes are open source and play any .riv file, so the fixes below are about how your code creates, pauses and releases the runtime. Smaller files help as well. See how to optimize Rive WASM bundle size for loading strategy.

Which Rive React package and renderer use less GPU?

For most React apps, Rive recommends @rive-app/react-canvas, which wraps the Canvas2D web runtime. Rive's renderer guide suggests the Canvas2D option for pages with several Rive instances, or when Rive Renderer-only features are not needed.

Package or variantUse it when
@rive-app/react-canvasMost apps, and pages with several instances
@rive-app/react-canvas-liteRive Text is not used, according to Rive's React overview
WebGL2 React variantA WebGL backing renderer is specifically needed

The WebGL2 variant gained an enableGPUCanvas parameter in recent release notes. Confirm the parameter in the notes for your installed version before you rely on it, since the package is updated often.

Measure on a mid-range phone or an older laptop rather than a fast desktop GPU, which hides the cost. If frames drop only on the weaker device, reduce the number of live instances first. Changing the renderer comes second.

How do you write a React component that releases its Rive instance?

The hook ties the element to the component's lifecycle, so the simplest leak-free pattern is one useRive call per visible mascot, a guard on the input, and no instances created outside React. Pass the value you want to drive, such as a mood number, through an effect.

import { useEffect } from 'react';
import { useRive, useStateMachineInput } from '@rive-app/react-canvas';

export function Mascot({ mood }) {
  const { rive, RiveComponent } = useRive({
    src: '/mascot.riv',
    stateMachines: 'CharacterStateMachine',
    autoplay: true,
  });
  const moodInput = useStateMachineInput(rive, 'CharacterStateMachine', 'mood');

  useEffect(function syncMood() {
    if (moodInput) moodInput.value = mood;
  }, [moodInput, mood]);

  return <RiveComponent />;
}
  • Guard the input. useStateMachineInput returns null until the Rive object is ready and the named input is found, so check it before writing.
  • Check the hook's status. Newer releases mark useStateMachineInput as deprecated in favour of data binding hooks, while Rive's docs still present it. Read the reference for your installed version before you build on it.
  • Use the right input name. A Charmotion file exposes a number input named mood in CharacterStateMachine, where 4 is sad and 1 is happy.
  • Keep instances out of module scope. A Rive object stored in a global variable outlives the page that created it.

If you construct a Rive instance yourself with a lower-level API, release it inside the effect's return function, using the cleanup method in that runtime's reference.

How do you pause Rive animations that are off screen or hidden?

Pause what the user cannot see. The browser's IntersectionObserver reports when a container scrolls out of view, and the visibilitychange event tells you when a tab goes to the background. Together they cover a mascot below the fold, a loader inside a modal, or a character in a tab the user switched away from.

  • Watch the container. When the observer reports that the container has left the viewport, pause the Rive instance. When it comes back, start playback again so the animation does not look frozen.
  • Watch the tab. Listen for the visibility change event on the document. Pause when the page is hidden and resume when it is visible.
  • Clean up on unmount. When the component leaves the screen, disconnect the observer and remove the visibility listener. Hand-written observers and listeners are the step most often left out, and they keep the old component alive.
  • Set it up once per instance. Attach these watchers after the Rive instance exists, and tie them to that instance's lifetime so a replaced instance does not keep its old watchers.

Test both cases: scroll the mascot out of view and back, then switch tabs and return. In each case the animation should stop while hidden and continue from where it paused.

How do you avoid Rive leaks in React Native, Flutter and Android?

Each mobile runtime has its own lifecycle, so the release step differs. The table lists the point where each runtime's view or controller should be released.

PlatformRuntimeRelease point
React Native@rive-app/react-native, built on NitroThe native runtime needs a development build, not Expo Go. Let the screen's unmount release the view, and test that pushing and popping screens does not add memory.
FlutterThe rive package, built on rive_nativeDispose any controller or file you created in the widget's dispose method.
Android (Compose)Rive's Android runtime with the Compose API, marked BetaKeep file loading tied to the composable's lifecycle, so leaving the screen removes it. Test screen transitions because the API is Beta.
Apple (SwiftUI or UIKit)The rive-ios Swift package with RiveViewModelHold the view model in a screen-owned object, so it is created once per screen and freed when the screen closes.

Mobile memory behaves differently from browser memory, so test navigation directly. Push and pop the screen 20 times, then compare memory in Xcode Instruments on iOS or the Android Studio profiler on Android. A flat line means the screen is releasing its runtime. A line that climbs by a fixed amount on every visit points to a leak.

For React Native and Expo projects, see Rive in Expo, and for native Android and Flutter setups see Rive in Android, Flutter and React Native.

How do you check that the fix worked?

Profile before and after the change, with the same steps each time. The goal is to confirm that memory returns to its starting level and that GPU work drops when the animation is off screen.

  1. Open the page and record memory with the Chrome DevTools Memory panel, then take a heap snapshot.
  2. Navigate to another page and back ten times, forcing a garbage collection between runs.
  3. Take a second heap snapshot and compare it. Look for retained Rive objects or detached canvas elements that grow with each visit.
  4. Open Chrome's Task Manager, which lists GPU memory per tab, and confirm it drops when the mascot leaves the screen.

Use this checklist before you ship:

  • One live Rive instance per visible mascot, with static images for repeated items.
  • Every instance released on unmount, with no references kept in module scope.
  • Off-screen and hidden playback paused, and resumed when visible again.
  • The renderer package matches what the page needs.
  • Files loaded once and reused, with the native view or controller freed on each platform.

How to prevent Rive animation memory leaks and high GPU use in a React or mobile app

  1. Count live instances. List every Rive animation on each screen. Keep one live instance for each visible mascot and replace repeated items in lists with static images.
  2. Choose the package. Use @rive-app/react-canvas for most React pages. Switch to the lite or WebGL2 variant only when Rive Text or a WebGL renderer is specifically needed.
  3. Create instances inside components. Call useRive once per component, guard useStateMachineInput for null, and avoid storing Rive objects in module scope.
  4. Pause hidden playback. Use IntersectionObserver and the visibilitychange event to pause off-screen and background animations, and remove both listeners in the effect cleanup.
  5. Release native views on mobile. In React Native, Flutter, Android Compose and Apple apps, release the view or controller when its screen closes, and test by pushing and popping the screen repeatedly.
  6. Profile before and after. Navigate away and back ten times, compare heap snapshots and GPU memory, and confirm the numbers return to the starting level.

Frequently asked questions

Does a Rive animation keep using memory after I leave the page?

It can, if the instance is never released. A hook-managed element is removed on unmount, so the usual risk is an instance you created by hand or a reference kept in a global variable. Navigate away and back, then check in DevTools whether memory returns to its starting level.

Do I need a paid Rive plan to fix memory or GPU problems?

No. Playback is handled by the runtimes, which are open source and play any .riv file. Plans affect exporting: free-plan exports play a Rive splash screen, while paid plans export without it. Check Rive's pricing page for current plan details.

Is Lottie lighter on the GPU than Rive?

It depends on the animation, the runtime and the device. Lottie and Rive trade off file size, interactivity and runtime cost differently. The performance comparison covers those trade-offs in more detail.

Why does my mascot stutter only on older phones?

Older devices have less GPU headroom, so a page that holds smooth frames on a laptop can drop frames on a phone. Reduce the number of live instances first, then check that the canvas is not larger than it appears on screen.

Sources

Related guides