详解flutter engine 那些没被释放的东西
由于flutter一直存在内存泄漏的问题,导致很多开发者不胜困扰,博主在0.9.4就开始对其代码内部内存问题在engine层面修改代码,得到解决,但是对于每个版本都需要跟随官方打包,对于开发者并不是很友好。
然而喜出望外的是,在后来的几个版本中,官方内置开发了手动释放内存的方式:smile_cat:
/** * destroy running context for an engine. * * this method can be used to force the flutterengine object to release all resources. * after sending this message, the object will be in an unusable state until it is deallocated. * accessing properties or sending messages to it will result in undefined behavior or runtime * errors. */ - (void)destroycontext;
翻译如下:
销毁引擎的运行上下文。 此方法可用于强制flutterengine对象释放所有资源。 发送此消息后,对象将处于不可用状态,直到解除分配为止。 访问属性或向其发送消息将导致未定义的行为或运行时错误。
但是
, 但是
, 但是
,(重要的事说三遍) 在flutter engine开发群里面,有群友反馈还有很多问题
- 无法完全释放内存
- 偶现崩溃
偶现崩溃的是什么鬼,暂时没有遇到,不好说。 之前博主遇到的崩溃是自己使用方式的问题,在fluttervc关闭之后还有任务在执行methodchannel,即还在调用plugin,这个可以在开发上避免。 值得注意的是,flutter中使用c++实现,自己对于内存管理并不是很好
内存问题自测如下
确实存在问题,还有将近30m没有被释放,查看一下当前内存对象,如下图
一个一个看还有那些没有被释放吧
android:lrucache
least recently used 近期最少使用算法。 内存管理的一种页面置换算法,对于在内存中但又不用的数据块(内存块)叫做lru,flutter engine 会根据哪些数据属于lru而将其移出内存而腾出空间来加载另外的数据。
dart::backgroundcomplier 对isolate编译优化的类
backgroundcompiler 在后台线程中运行优化编译的类。 实现:每个隔离一个任务,它与拥有isolate一起消失,后台编译器中没有osr编译。
dart::bin::socket
vm和开发平台通信的机制,比如jit即时编译的dill文件,通过socket传递给dart vm,vm通过rpc加载文件,重置线程,从而实现hotreload热重载
dart::boolprameter
- dart::enumparameter
- dart::idparameter
- dart::idparameter
- dart::xxxprameter
定义在dart vm,service.cc中,都继承自methodparameter,做对应参数校验,参数解析用。编译dart文件用的
dart::osthread
在dart 运行时负责操作系统线程,创建线程,移除线程,线程查找与管理。 如下图
flutterengineregistrar 注册使用key注册plugin的地方,所有plugin调用dart底层的方法都会通过 handlemethodcall 回调给使用者, 其初始化的地方是引起内存泄漏的地方
- (instancetype)initwithplugin:(nsstring*)pluginkey flutterengine:(flutterengine*)flutterengine { self = [super init]; nsassert(self, @"super init cannot be nil"); _pluginkey = pluginkey;// [pluginkey retain]; _flutterengine = flutterengine;// [flutterengine retain]; return self; }
此处有一篇文章介绍,解决engine的循环引用文章
flutterstandardmethodcodec 标准方法编解码
flutterstringcodec string编解码 flutterjsonmessagecodec json编解码
不看不知道,一看吓一跳,也竟然是个单例,当然不会被释放了,也能理解,在flutter中用到jsonmssage的地方很多,用不着每次都初始化
代码实现的地方
@implementation flutterjsonmessagecodec + (instancetype)sharedinstance { static id _sharedinstance = nil; if (!_sharedinstance) { _sharedinstance = [flutterjsonmessagecodec new]; } return _sharedinstance; }
std:share_ptrxxx 共享指针
指针获取 flutter isolate service dartvm symbolmapping
~~ 文章完 ~~
如果你想深入讨论flutter的问题,欢迎加入我们的qq群 217429001
完整测试代码如下
#import "fluttertesterviewcontroller.h" #import <flutter/flutter.h> #import "generatedpluginregistrant.h" @interface fluttertesterviewcontroller () @property (nonatomic, weak) flutterviewcontroller * ctr; @property (nonatomic, weak) flutterengine * engine; @end @implementation fluttertesterviewcontroller - (void)viewdidload { [super viewdidload]; float y = 210; [self createbutton:@"加载boundle资源" frame:cgrectmake(80.0, y, 160.0, 40.0) action:@selector(handleboundleresource )]; y += 40.0 + 10; [self createbutton:@"autorelease" frame:cgrectmake(80.0, y, 160.0, 40.0) action:@selector(handleautorelase)]; nsarray * paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring * path = [[paths objectatindex:0] stringbyappendingpathcomponent:@"flutter_assets"] ; nslog(@"path: %@",path); } -(void)handlenetworkresource:(uibutton *)button{ } /** * 加载boundle资源 */ - (void)handleboundleresource { flutterdartproject * dart = [[flutterdartproject alloc] init]; flutterengine * engine = [[flutterengine alloc] initwithname:@"ios.dart.flutter" project:dart]; [engine runwithentrypoint:nil]; flutterviewcontroller* flutterviewcontroller = [[flutterviewcontroller alloc] initwithengine:engine nibname:nil bundle:nil]; [generatedpluginregistrant registerwithregistry:flutterviewcontroller]; [self addbackbutton:flutterviewcontroller]; [flutterviewcontroller setinitialroute:@"route1"]; [self presentviewcontroller:flutterviewcontroller animated:yes completion:nil]; self.engine = engine; } -(void)handleautorelase{ flutterbasicmessagechannel* channel; flutterengine * engine; @autoreleasepool { flutterviewcontroller* flutterviewcontroller = [[flutterviewcontroller alloc] init]; channel = flutterviewcontroller.engine.systemchannel; engine = flutterviewcontroller.engine; nslog(@"engine111:%@",engine); } nslog(@"engine222:%@",engine); [channel sendmessage:@"hello!"]; [channel setmessagehandler:^(id _nullable message, flutterreply _nonnull callback) { }]; } -(void)addbackbutton:(uiviewcontroller *)flutterviewcontroller{ dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(2 * nsec_per_sec)), dispatch_get_main_queue(), ^{ uibutton * btn = [uibutton buttonwithtype:uibuttontypesystem]; [btn settitle:@"关闭" forstate:uicontrolstatenormal]; btn.frame = cgrectmake(10, 100, 50, 30); [btn addtarget:self action:@selector(buttontap:) forcontrolevents:uicontroleventtouchupinside]; [flutterviewcontroller.view addsubview:btn]; self.ctr = flutterviewcontroller; }); } -(void)buttontap:(id)sender{ // [self.navigationcontroller popviewcontrolleranimated:yes]; __weak __typeof(self)weakself = self; [self.ctr dismissviewcontrolleranimated:yes completion:^{ [weakself.engine destroycontext]; }]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } -(uibutton *)createbutton:(nsstring *)title frame:(cgrect)frame action:(sel)selector{ uibutton *button = [uibutton buttonwithtype:uibuttontypecustom]; [button addtarget:self action:selector forcontrolevents:uicontroleventtouchupinside]; [button settitle:title forstate:uicontrolstatenormal]; uicolor * bgcolor = [uicolor colorwithred:arc4random()%256/255. green:arc4random()%256/255. blue:arc4random()%256/255. alpha:1]; [button setbackgroundcolor:bgcolor]; button.frame = frame; [self.view addsubview:button]; return button; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: vue回到顶部监听滚动事件详解
下一篇: Vue组件间通信 Vuex的用法解析