Skip to content

Instantly share code, notes, and snippets.

@jezell
Created June 1, 2024 01:06
Show Gist options
  • Save jezell/1fdb298ffc9409c7360189760bd74f9f to your computer and use it in GitHub Desktop.
Save jezell/1fdb298ffc9409c7360189760bd74f9f to your computer and use it in GitHub Desktop.
Memory Monitor
import "package:flutter/material.dart";
import "package:google_fonts/google_fonts.dart";
import "memory_usage.dart" if (dart.library.html) 'memory_usage_web.dart';
import "dart:async";
const showMemMonitor = bool.fromEnvironment("SHOW_MEM_MONITOR");
class MemoryMonitor extends StatefulWidget {
const MemoryMonitor({super.key});
@override
State createState() => _MemoryMonitorState();
}
class _MemoryMonitorState extends State<MemoryMonitor> {
@override
void initState() {
super.initState();
bytes = getMemoryUsageBytes();
timer = Timer.periodic(const Duration(seconds: 1), (t) {
setState(() {
bytes = getMemoryUsageBytes();
});
});
}
late Timer timer;
int bytes = 0;
@override
void dispose() {
super.dispose();
timer.cancel();
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: switch (bytes) {
> 1024 * 1024 * 1024 => Colors.red,
> 512 * 1024 * 1024 => Colors.orange,
_ => Colors.green,
},
borderRadius: BorderRadius.circular(20)),
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 2),
child: Text("${(bytes / 1024.0 / 1024.0).ceil()}MB",
style: GoogleFonts.heebo(
fontWeight: FontWeight.w500,
fontSize: 11,
color: const Color.fromARGB(0xff, 0xff, 0xff, 0xff))));
}
}
int getMemoryUsageBytes() {
return 0;
}
import "dart:js_interop";
@JS('window.flutterCanvasKit.HEAP8.byteLength')
external JSNumber get _canvasKitMemory;
int getMemoryUsageBytes() {
return _canvasKitMemory.toDartInt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment