-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak with Flutter Session Replay #142
Comments
Asked questions on the link. |
I have the same error message but when I have a TextFormField on the page and SessionReplay activated. Once the focus in on the TextFormField I get that error message every seconds flutter: Error: Failed to capture screenshot. My page also has SVG but removing them didn't change anything to the message - I don't know if it leads to a memory leak. class OnboardingPage extends StatelessWidget {
const OnboardingPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Form(
child: Column(
children: [
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Enter your character name',
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a name';
}
return null;
},
),
],
),
),
),
);
}
} my config final config =
PostHogConfig('xxx')
..debug = kDebugMode
..captureApplicationLifecycleEvents = true
..host = 'https://us.i.posthog.com'
..sessionReplay = true
..sessionReplayConfig.maskAllTexts = false
..sessionReplayConfig.maskAllImages = false; A simple example of how I use it in my project using 4.9.1. |
this is not an error btw, it's just logging. |
@JobiJoba this looks like a different issue, mind creating a new issue and providing an MRE? a sample where I can just run and reproduce the issue since I can't reproduce it myself? Thanks. |
Hello everyone, apologies for the delay. Main import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';
import 'package:vize/core/config/analytics.dart';
import 'package:vize/core/config/env.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Env.load();
await AnalyticsConfig.configurePostHog();
runApp(
ChangeNotifierProvider<AppViewModel>(
create: (_) => AppViewModel(),
child: const App(),
),
);
}
class AppViewModel with ChangeNotifier {
AppViewModel() {
Timer.periodic(const Duration(milliseconds: 100), (_) {
notifyListeners();
});
}
}
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return Column(children: <Widget>[
SvgPicture.asset('assets/icons/profile/0.svg'),
Consumer<AppViewModel>(builder: (BuildContext context, AppViewModel viewModel, _) {
return SizedBox.shrink();
})
]);
}
} PostHog Configuration import 'package:posthog_flutter/posthog_flutter.dart';
import 'package:vize/core/config/env.dart';
class AnalyticsConfig {
AnalyticsConfig._();
static Future<void> configurePostHog() async {
final PostHogConfig config = PostHogConfig(Env.posthogApiKey)
..debug = false
..captureApplicationLifecycleEvents = false
..host = 'https://us.i.posthog.com'
..sessionReplay = true
..sessionReplayConfig.maskAllTexts = false
..sessionReplayConfig.maskAllImages = false
..sessionReplayConfig.throttleDelay = const Duration(milliseconds: 500);
await Posthog().setup(config);
}
}
Tested on iOS 18.1 flutter version: 3.27.3 Let me know if further details or adjustments to the MRE are needed, and thanks for your help in investigating this! |
@waskalien, which tools are you using to check for memory increase? standard memory view? |
Hi @marandaneto, I believe the issue does not appear in Flutter DevTools, but it is clearly visible in Xcode’s Memory tab. |
@waskalien https://github.com/waskalien/posthog-session-replay-memory-leak/blob/68b7b0a7d3abb39634cd3c3d378a066a01491f54/lib/main.dart#L39-L41 |
@marandaneto Yes, this is realistic in our case, as our app includes various real-time animations and a specific animation that requires a periodic timer running at 60 FPS. As I mentioned before, if we remove the periodic timer, the memory leak disappears, which is the core issue here. This is not just higher memory consumption, it’s a true memory leak. To illustrate: even if we do nothing and just display a static SVG with no movement, the app’s memory usage keeps increasing indefinitely. Wouldn’t you consider that a memory leak? |
Maybe something is not correctly disposed, indeed. (I will take a look) |
@marandaneto Thanks for looking into it! After stopping the timer, some of the allocated memory is freed, but not all of it. For example, in a 50-second test, starting at 113 MB, memory increases to 125 MB. Once the timer is stopped, it drops to 119 MB, leaving a 6 MB residual increase that isn’t released. During the timer’s execution, memory usage spikes even higher, and we can observe periodic small memory releases every few seconds. It forms a stair-step pattern, where some memory is freed, but not entirely. Let me know if you need more details |
![]() So using the memory view on Dart/Flutter, the memory is pretty stable. |
Improvements such as #165 could help here, improving performance overall and not only the leak, I mean. |
I found something here #166 but it didnt improve much so its not the cause |
So I finally narrowed down the issue to this line. The problem isn't really memory leaking but a similar side effect. We need an image so we have to call The problem is that your screen is updating way too often, so we have to generate way too many images and the Dart/Flutter GC isn't running as often as it should, so a lot of images are in memory, because calling There's no way to call the GC manually sadly. I think the best approach here is for you to increase your |
@waskalien thanks for that. |
@marandaneto I think it's because the image is passed to ![]() |
nope, |
Okay, then I was probably wrong. I thought that since the image was passed to |
Description
from: https://posthog.com/questions/memory-leak-with-flutter-session-replay
The text was updated successfully, but these errors were encountered: