Flutter-检测内存泄漏

时间:2018-08-26 09:26:46

标签: flutter

我有点困惑,因为我一直在思考,因为没有weak的概念(如果我是对的),就不会出现内存泄漏。
我正在iOS设备上运行它。
我正在尝试播放视频并预先初始化一些视频,以便用户可以立即看到它。
为此,我准备了六个VideoPlayerController,并使它们始终在当前视频播放时被初始化。
当前的一个旁边还有另外三个初始化的VideoPlayerController,而当前的一个之前还有另外两个初始化的Future, async, await,如下图所示。 按照这种逻辑,我可以非常流畅地来回播放视频。但是播放了大约十个视频后,由于内存问题,应用程序崩溃了。 我尝试了所有功能NotificationListener,但仍然有很多记忆。 我不确定,但是可能是onNotification
由于bool返回Future而不是main thread或 这和 class _SwiperScreenState extends State<SwiperScreen> { VideoPlayerController _firstController; VideoPlayerController _secondController; VideoPlayerController _thirdController; VideoPlayerController _fourthController; VideoPlayerController _fifthController; VideoPlayerController _sixthController; List<VideoPlayerController> _controllers; List<String> urls = [ 'https://firebasestorage.googleapis.com/v0/b/waitingboy-34497.appspot.com/o/video%2F8-21%2F1534825377992OsfJfKsdf90K8sf?alt=media&token=12245ee4-1598-4f7e-ba28-a9eb72ca474e', 'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4', 'https://firebasestorage.googleapis.com/v0/b/waitingboy-34497.appspot.com/o/video%2F8-21%2F1534825377992OsfJfKsdf90K8sf?alt=media&token=12245ee4-1598-4f7e-ba28-a9eb72ca474e', 'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4', ]; int currentIndex = 0; //refer to current playing controller index int videosIndex = 0; //refer to current playing urls index bool _didGetNotification(ScrollNotification notification) { if (notification is UserScrollNotification) { if (notification.direction.toString() == 'ScrollDirection.reverse') { //swipe to left so add one more video videosIndex++; //modify index so that always in the range of 0 ~ 5. if (currentIndex <= 2) { final int prepareIndex = currentIndex + 3; urls.add( 'https://firebasestorage.googleapis.com/v0/b/waitingboy-34497.appspot.com/o/video%2F8-21%2F1534825377992OsfJfKsdf90K8sf?alt=media&token=12245ee4-1598-4f7e-ba28-a9eb72ca474e'); _initVideo(urls[videosIndex], prepareIndex); } else { final int prepareIndex = (currentIndex + 3) - 6; urls.add( 'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4'); _initVideo(urls[videosIndex], prepareIndex); } } if (notification.direction.toString() == 'ScrollDirection.forward') { //swipe to right so back one more video videosIndex--; //modify index so that always in the range of 0 ~ 5 . if (currentIndex >= 2) { final int videoIndex = videosIndex - 2; final int prepareIndex = currentIndex - 2; _initVideo(urls[videoIndex], prepareIndex); } else { final int videoIndex = videosIndex - 2; final int prepareIndex = 4 + currentIndex; _initVideo(urls[videoIndex], prepareIndex); } } } return true; } Future _initVideo(String url, int initIndex) async { if (_controllers[initIndex] != null) { await _controllers[initIndex].dispose(); } _controllers[initIndex] = new VideoPlayerController.network(url); await _controllers[initIndex].initialize().then((_) async => await _controllers[initIndex].setLooping(true)); setState(() {}); } Future _initFirstThree() async { for (int i = 1; i < urls.length; i++) { await _initVideo(urls[i], i); } } @override void initState() { _controllers = [ _firstController, _secondController, _thirdController, _fourthController, _fifthController, _sixthController ]; _initVideo(urls[0], 0).then((_) => _controllers[0].play()); _initFirstThree(); super.initState(); } @override void deactivate() { _controllers[currentIndex].setVolume(0.0); _controllers[currentIndex].pause(); super.deactivate(); } @override void dispose() { _controllers.forEach((con) { con.dispose(); }); super.dispose(); } @override Widget build(BuildContext context) { return new Scaffold( appBar: new AppBar( title: new Text('Swiper'), actions: <Widget>[ new IconButton( icon: new Icon(Icons.disc_full), onPressed: () { Navigator .of(context) .push(MaterialPageRoute(builder: (context) => Dissmiss())); }, ) ], ), body: new NotificationListener( onNotification: _didGetNotification, child: new Swiper( itemCount: 6, itemBuilder: (BuildContext context, int index) { return _controllers[index].value.initialized ? new AspectRatio( aspectRatio: _controllers[index].value.aspectRatio, child: new VideoPlayer(_controllers[index]), ) : new Center(child: new CircularProgressIndicator()); }, loop: urls.length > 6 ? true : false, onIndexChanged: (i) async { currentIndex = i; final int pauseIndex = i == 0 ? 5 : i - 1; await _controllers[pauseIndex].pause().then((_) async { await _controllers[i].play(); }); }, ), ), ); } } 有关系吗?
有人知道如何解决此内存问题吗?

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/appbackground"
android:orientation="vertical"
android:weightSum="10">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2">


    <TextView
        android:id="@+id/textgird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="WAJunk Cleaner"
        android:textColor="#fff"
        android:textSize="34sp" />

</RelativeLayout>

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="8"
    android:alignmentMode="alignMargins"
    android:baselineAligned="false"
    android:columnCount="2"
    android:columnOrderPreserved="false"
    android:padding="14dp"
    android:rowCount="4">

    <!-- ROW1 -->

    <android.support.v7.widget.CardView
        android:id="@+id/android_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/photoicon" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

        android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/whatsapp_images_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="10sp" />

        </LinearLayout>

    </android.support.v7.widget.CardView>


    <!--ROW2 -->

    <android.support.v7.widget.CardView
        android:id="@+id/whatsapp_video"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/videoicon" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

           android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/whatsapp_video_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="12sp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>

    <!-- ROW3 -->

    <android.support.v7.widget.CardView
        android:id="@+id/whatsapp_databases"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/database" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

      android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/whatsapp_databases_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="12sp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>

    <!--ROW4 -->

    <android.support.v7.widget.CardView
        android:id="@+id/android_profile"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/profilepic" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

       android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">


            <TextView
                android:id="@+id/whatsapp_dp_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="12sp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>

    <!-- ROW5 -->

    <android.support.v7.widget.CardView
        android:id="@+id/whatsapp_wallpaper"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/wallpapericon" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

           android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/whatsapp_wallpaper_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="12sp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>


    <!--ROW6 -->

    <android.support.v7.widget.CardView
        android:id="@+id/whatsapp_voice_notes"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/voicenote" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

           android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/whatsapp_voice_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="11sp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>


    <!-- ROW7 Documents-->

    <android.support.v7.widget.CardView
        android:id="@+id/android_documents"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/documents" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

           android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/whatsapp_documents_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="12sp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>


    <!--ROW8 DataBase -->

    <android.support.v7.widget.CardView
        android:id="@+id/whatsapp_audio"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_rowWeight="1"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center_vertical"
            android:layout_margin="10dp"
            android:orientation="vertical"
            android:paddingBottom="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/musicicon" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

          android:layout_gravity="center_horizontal|center_vertical|bottom"
            android:orientation="vertical">

            <TextView
                android:id="@+id/whatsapp_audio_sub_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:textColor="#000"
                android:textSize="12sp" />


        </LinearLayout>

    </android.support.v7.widget.CardView>


</GridLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner_home_footer">      
 </com.google.android.gms.ads.AdView>
 </LinearLayout>

0 个答案:

没有答案