Last active
August 2, 2025 10:04
-
-
Save 423u5/6f3c36a17512651d8f65338a8c6ca56d to your computer and use it in GitHub Desktop.
Simple Android FPS monitor utility using Choreographer to track the frame rate and dropped frames in real time. Monitors FPS and dropped frames over a configurable interval (default: 500 ms). Supports multiple listeners with weak references to avoid memory leaks. Can adapt to the device’s actual refresh rate automatically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.os.Handler; | |
| import android.os.Looper; | |
| import android.view.Choreographer; | |
| import android.view.Display; | |
| import android.view.WindowManager; | |
| import java.lang.ref.WeakReference; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| public class FPSMonitor { | |
| private static class Holder { | |
| private static final FPSMonitor INSTANCE = new FPSMonitor(); | |
| } | |
| public static FPSMonitor getInstance() { | |
| return Holder.INSTANCE; | |
| } | |
| private FPSMonitor() {} | |
| private final Set<WeakReference<Listener>> listeners = new HashSet<>(); | |
| private final Handler mainHandler = new Handler(Looper.getMainLooper()); | |
| private Choreographer choreographer; | |
| //every 500ms | |
| private long updateIntervalNs = 500_000_000L; | |
| private long frameIntervalNs = 16_666_667L; | |
| private int frameCount = 0; | |
| private int droppedFrame = 0; | |
| private long lastTimeFps = 0L; | |
| private long lastTimeFrameDrop = 0L; | |
| private boolean isRunning = false; | |
| private final Choreographer.FrameCallback frameCallback = new Choreographer.FrameCallback() { | |
| @Override | |
| public void doFrame(long frameTimeNanos) { | |
| frameCount++; | |
| if (lastTimeFps == 0L) { | |
| lastTimeFps = frameTimeNanos; | |
| } | |
| if (lastTimeFrameDrop != 0L) { | |
| long timeDiff = frameTimeNanos - lastTimeFrameDrop; | |
| int expectedFrames = (int) (timeDiff / frameIntervalNs); | |
| if (expectedFrames > 1) { | |
| droppedFrame += (expectedFrames - 1); | |
| } | |
| } | |
| lastTimeFrameDrop = frameTimeNanos; | |
| if (frameTimeNanos - lastTimeFps >= updateIntervalNs) { | |
| frameCount = (int) (frameCount * (1_000_000_000f / updateIntervalNs)); | |
| for (WeakReference<Listener> ref : listeners) { | |
| Listener listener = ref.get(); | |
| if (listener == null) continue; | |
| listener.onResult(frameCount, droppedFrame); | |
| } | |
| frameCount = 0; | |
| droppedFrame = 0; | |
| lastTimeFps = frameTimeNanos; | |
| if (listeners.isEmpty() && isRunning) { | |
| isRunning = false; | |
| choreographer.removeFrameCallback(frameCallback); | |
| return; | |
| } | |
| } | |
| choreographer.postFrameCallback(frameCallback); | |
| } | |
| }; | |
| public void addListener(Listener listener) { | |
| if (listener == null) return; | |
| mainHandler.post(() -> { | |
| if (choreographer == null) { | |
| choreographer = Choreographer.getInstance(); | |
| } | |
| listeners.add(new WeakReference<>(listener)); | |
| if (isRunning) return; | |
| isRunning = true; | |
| choreographer.postFrameCallback(frameCallback); | |
| }); | |
| } | |
| public void removeListener(Listener listener) { | |
| mainHandler.post(() -> { | |
| for (WeakReference<Listener> ref : listeners) { | |
| if (ref.get() == listener) { | |
| listeners.remove(ref); | |
| break; | |
| } | |
| } | |
| if (listeners.isEmpty() && isRunning) { | |
| isRunning = false; | |
| choreographer.removeFrameCallback(frameCallback); | |
| } | |
| }); | |
| } | |
| public FPSMonitor updateInterval(long intervalNs) { | |
| updateIntervalNs = intervalNs; | |
| return this; | |
| } | |
| public FPSMonitor setRefreshRate(float refreshRate) { | |
| frameIntervalNs = (long)(1_000_000_000 / refreshRate); | |
| return this; | |
| } | |
| public FPSMonitor useDeviceRefreshRate(Context context) { | |
| WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
| Display display = windowManager.getDefaultDisplay(); | |
| return setRefreshRate(display.getRefreshRate()); | |
| } | |
| public interface Listener { | |
| void onResult(int fps, int droppedFrame); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment