gpt4 book ai didi

flutter - 为什么 flutter image 在 widget 关闭时没有释放内存

转载 作者:行者123 更新时间:2023-12-05 06:00:26 35 4
gpt4 key购买 nike

最近遇到一个问题,我的flutter(2.2.0) app运行一段时间后老是闪退,后来debbuing发现文章详情页有很多图片时,多次关闭重新打开,内存一直在增加,为什么关闭文章详情页后图片没有处理?我尝试通过这种方式在退出文章详细信息页面之前清除图像缓存(这不应该使用任何图像缓存,不是一个好的做法):

if (PaintingBinding.instance != null && PaintingBinding.instance!.imageCache != null) {
PaintingBinding.instance!.imageCache!.clear();
PaintingBinding.instance!.imageCache!.clearLiveImages();
}

但现在日志显示此信息:

I/rth.dolphin.de( 9117): Explicit concurrent copying GC freed 4578(176KB) AllocSpace objects, 1(20KB) LOS objects, 49% free, 2412KB/4824KB, paused 574us total 8.073ms
I/rth.dolphin.de( 9117): Explicit concurrent copying GC freed 4578(176KB) AllocSpace objects, 1(20KB) LOS objects, 49% free, 2412KB/4824KB, paused 734us total 7.350ms
I/rth.dolphin.de( 9117): Explicit concurrent copying GC freed 4578(176KB) AllocSpace objects, 1(20KB) LOS objects, 49% free, 2412KB/4824KB, paused 612us total 8.267ms
I/rth.dolphin.de( 9117): Explicit concurrent copying GC freed 4578(176KB) AllocSpace objects, 1(20KB) LOS objects, 49% free, 2412KB/4824KB, paused 746us total 7.442m

这是处理这个问题的正确方法,看到使用这种方法清除图像缓存有副作用。我该怎么做才能解决它?顺便说一句,这是我捕捉到的图像变化趋势:

enter image description here

我尝试使用此代码片段来监控图像缓存:

var imageCache= PaintingBinding.instance!.imageCache;

print("dd:"+imageCache!.currentSizeBytes.toString());
print("size:" + imageCache.currentSize.toString());

发现文章详情页关闭再打开时图片缓存没有变化,但内存还在上升。这是显示图像的关键代码:

if (item.content != "")
Html(
data: item.content,
style: {
"body": Style(
fontSize: FontSize(19.0),
),
},
customImageRenders: defaultImageRenders,
onLinkTap: (String? url, RenderContext context, Map<String, String> attributes, dom.Element? element) {
CommonUtils.launchUrl(url);
}),

图像在 html 内容中,我正在使用 flutter html显示它。这是我的完整代码:

import 'package:cruise/src/common/article_action.dart';
import 'package:cruise/src/common/helpers.dart';
import 'package:cruise/src/common/net/rest/http_result.dart';
import 'package:cruise/src/common/repo.dart';
import 'package:cruise/src/common/utils/common_utils.dart';
import 'package:cruise/src/models/Channel.dart';
import 'package:cruise/src/models/Item.dart';
import 'package:cruise/src/models/api/fav_status.dart';
import 'package:cruise/src/models/api/upvote_status.dart';
import 'package:cruise/src/page/channel/channelpg_component/page.dart';
import 'package:cruise/src/page/home/components/articledetail_component/action.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_html/style.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:html/dom.dart' as dom;

import 'state.dart';

Widget buildView(ArticleDetailState state, Dispatch dispatch, ViewService viewService) {
Item item = state.article;
BuildContext context = viewService.context;
Offset? _initialSwipeOffset;
Offset? _finalSwipeOffset;

void _onHorizontalDragStart(DragStartDetails details) {
_initialSwipeOffset = details.globalPosition;
}

void _onHorizontalDragUpdate(DragUpdateDetails details) {
_finalSwipeOffset = details.globalPosition;
}

void _onHorizontalDragEnd(DragEndDetails details) {
if (_initialSwipeOffset != null) {
final offsetDifference = _initialSwipeOffset!.dx - _finalSwipeOffset!.dx;
if (offsetDifference < 0) {
if (PaintingBinding.instance != null && PaintingBinding.instance!.imageCache != null) {
// https://mp.weixin.qq.com/s/yUm4UFggYLgDbj4_JCjEdg
// https://musicfe.dev/flutter/
PaintingBinding.instance!.imageCache!.clear();
PaintingBinding.instance!.imageCache!.clearLiveImages();
}
Navigator.pop(context);
}
}
}

void touchUpvote(String action, UpvoteStatus upvoteStatus) async {
HttpResult result = (await ArticleAction.upvote(articleId: item.id.toString(), action: action))!;

if (result.result == Result.error) {
Fluttertoast.showToast(
msg: "点赞失败",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
} else {
if (upvoteStatus.statusCode == "upvote") {
dispatch(ArticleDetailActionCreator.onVote(UpvoteStatus.UPVOTE));
}
if (upvoteStatus.statusCode == "unupvote" && item.upvoteCount > 0) {
dispatch(ArticleDetailActionCreator.onVote(UpvoteStatus.UNUPVOTE));
}
Fluttertoast.showToast(
msg: upvoteStatus.statusCode == "upvote" ? "点赞成功" : "取消点赞成功",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
}

void touchFav(String action, FavStatus favStatus) async {
HttpResult result = (await ArticleAction.fav(articleId: item.id.toString(), action: action))!;

if (result.result == Result.error) {
Fluttertoast.showToast(
msg: favStatus.statusCode == "fav" ? "添加收藏失败" : "取消收藏失败",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
} else {
if (favStatus.statusCode == "fav") {
dispatch(ArticleDetailActionCreator.onFav(FavStatus.FAV));
}
if (favStatus.statusCode == "unfav" && item.favCount > 0) {
dispatch(ArticleDetailActionCreator.onFav(FavStatus.UNFAV));
}
Fluttertoast.showToast(
msg: favStatus.statusCode == "fav" ? "添加收藏成功" : "取消收藏成功",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.red,
textColor: Colors.white,
fontSize: 16.0);
}
}

void navToChannelDetail() async {
Channel channel = (await Repo.fetchChannelItem(int.parse(item.subSourceId)))!;
var data = {'name': "originalstories", "channel": channel};
Widget page = ChannelpgPage().buildPage(data);
Navigator.push(
context,
MaterialPageRoute(builder: (context) => page),
);
}

/// 是否是编辑选择频道链接显示不同的颜色
TextStyle getDomainStyle(Item article) {
if (article.editorPick == 1) {
return new TextStyle(color: Color(0xFFFFA826), fontSize: 15);
} else {
return new TextStyle(color: Color(0xFF0A0A0A), fontSize: 15);
}
}

ImageSourceMatcher base64UriMatcher() =>
(attributes, element) => attributes["src"] != null && attributes["src"]!.startsWith("data:image") && attributes["src"]!.contains("base64,");

Widget loadingWidget() {
return Center(
child: Container(
height: 400.0,
width: 120.0,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
child: CircularProgressIndicator(),
height: 50.0,
width: 50.0,
)
],
)),
);
}

final Map<ImageSourceMatcher, ImageRender> defaultImageRenders = {
base64UriMatcher(): base64ImageRender(),
assetUriMatcher(): assetImageRender(),
networkSourceMatcher(extension: "svg"): svgNetworkImageRender(),
networkSourceMatcher(): networkImageRender(height: 400, loadingWidget: loadingWidget),
};

SingleChildScrollView buildListView(Item item, BuildContext context) {
return SingleChildScrollView(
key: PageStorageKey("detail" + item.id),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
InkWell(
onTap: () => CommonUtils.launchUrl(item.link),
child: Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
child: Text(
item.title,
style: Theme.of(context).textTheme.headline5!.copyWith(
fontWeight: FontWeight.w600,
),
),
),
),
),
if (item.domain != "")
Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: InkWell(
onTap: () async {
navToChannelDetail();
},
child: Text(
item.domain,
style: getDomainStyle(item),
)),
),
InkWell(
onTap: () {},
child: RichText(
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: item.author,
style: Theme.of(context).textTheme.caption,
),
TextSpan(
text: " ${String.fromCharCode(8226)} ",
style: Theme.of(context).textTheme.caption,
),
TextSpan(
text: item.ago,
style: Theme.of(context).textTheme.caption,
),
],
),
),
),
if (item.content != "")
Html(
data: item.content,
style: {
"body": Style(
fontSize: FontSize(19.0),
),
},
customImageRenders: defaultImageRenders,
onLinkTap: (String? url, RenderContext context, Map<String, String> attributes, dom.Element? element) {
CommonUtils.launchUrl(url);
}),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Row(
children: [
if (item.isFav == 1)
IconButton(
icon: Icon(Icons.bookmark, color: Theme.of(context).primaryColor),
onPressed: () => touchFav("unfav", FavStatus.UNFAV),
),
if (item.isFav != 1)
IconButton(
icon: Icon(Icons.bookmark),
onPressed: () => touchFav("fav", FavStatus.FAV),
),
Padding(
padding: const EdgeInsets.only(left: 0.0),
child: Text(
"${item.favCount}",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption!.copyWith(
color: Theme.of(context).primaryColor,
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(bottom: 0.0),
child: Row(
children: [
if (item.isUpvote == 1)
IconButton(
icon: Icon(Icons.thumb_up, color: Theme.of(context).primaryColor),
onPressed: () => touchUpvote("unupvote", UpvoteStatus.UNUPVOTE),
),
if (item.isUpvote != 1)
IconButton(
icon: Icon(Icons.thumb_up),
onPressed: () => touchUpvote("upvote", UpvoteStatus.UPVOTE),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
"${item.upvoteCount}",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption!.copyWith(
color: Theme.of(context).primaryColor,
),
),
),
],
),
),
],
),
IconButton(
icon: Icon(
Feather.share_2,
),
onPressed: () => handleShare(id: item.id, title: item.title, postUrl: item.link),
),
],
),
],
));
}

var imageCache= PaintingBinding.instance!.imageCache;

print("dd:"+imageCache!.currentSizeBytes.toString());
print("size:" + imageCache.currentSize.toString());

return GestureDetector(
onHorizontalDragStart: _onHorizontalDragStart,
onHorizontalDragUpdate: _onHorizontalDragUpdate,
onHorizontalDragEnd: _onHorizontalDragEnd,
child: Container(
constraints: BoxConstraints(
minHeight: MediaQuery.of(context).size.height * 0.9,
),
color: Theme.of(context).scaffoldBackgroundColor,
child: Padding(
padding: const EdgeInsets.all(16.0),
child: buildListView(item, context),
),
));
}

最新更新:清除图片缓存时仍然触发OOM,图片好像没有从内存中卸载,加载更多图片,内存增加更多(内存永远不会被GC)。

最佳答案

我遇到了同样的问题,将你的问题添加为书签等待答案,最近我找到了一个简单的方法来解决它。

基本思想是为每个要显示的图像使用 ImageProvider 并使用 precacheImage 函数将其放入内存,然后您可以轻松地将其从内存中删除在 ImageProvider

上调用 evict 函数
// any class that extend ImageProvider
ImageProvider().evict();
// example
MemoryImage(bytes).evict();

完整的工作代码作为答案会太长。但我已经向 pub.dev 发布了一个包,它可以做同样的事情并具有其他功能 disposable_cached_images .

您可以查看源代码以更好地理解实现。

编辑:

经过更多测试,事实证明在图像提供程序上调用 evict 有时不起作用,尤其是在 resolve 期间调用它时。所以我进行了更改,发现使用 ui.image 更好直接没有提供者图像。

ui.image.dispose()效果很好。

关于flutter - 为什么 flutter image 在 widget 关闭时没有释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67667590/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com