uni-app live-pusher组件在安卓APP中关闭页面不会释放内存

uni-app live-pusher组件在安卓APP中关闭页面不会释放内存

开发环境 版本号 项目创建方式
Mac 26.2 HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Mac

HBuilderX类型:正式

HBuilderX版本号:4.76

手机系统:Android

手机系统版本号:Android 7.1.1

手机机型:固件

页面类型:nvue

vue版本:vue2

打包方式:云端

示例代码:

<live-pusher id='livePusher' ref="livePusher" class="livePusher" url=""
mode="SD" :muted="true"  :enable-camera="true" :auto-focus="true" :device-position="inWarehousePhoto" :beauty="0" :whiteness="0"
aspect="1:1" @statechange="statechange" @netstatus="netstatus" @error = "error" v-if="showLive"></live-pusher>  
onUnload() {
uni.$off('onWeight', this.onWeight);
clearInterval(liveTime)
// 先移除 live-pusher 组件,触发底层销毁
try {
this.showLive = false;
} catch (e) {}
// clearTimeout(scanTimeout)
// requestTask && requestTask.abort();
this.productList=null;
this.printdetailed=null;  

// 完善资源释放:停止推流、预览、关闭上下文  
if(this.context) {  
    try {  
        this.stop();  
        this.stopPreview();  
        this.pause();  
        // this.close();  
    } catch(e) {  
        console.log('资源释放失败:', e);  
    }  
}
}

操作步骤:

  • 重复进入页面,内存会一直占用

预期结果:

  • 关闭后一段时间自动回收

实际结果:

  • 内存会一直占用

bug描述:

  • 一个页面包含了这个组件后,内存会一直占有,除非杀死APP,如果去掉这个组件就正常

更多关于uni-app live-pusher组件在安卓APP中关闭页面不会释放内存的实战教程也可以访问 https://www.itying.com/category-93-b0.html

7 回复

该bug反馈内容基本完整但存在关键缺失。BUG描述清晰说明了live-pusher在安卓APP关闭页面后内存未释放的问题,但未明确说明是否使用nvue页面(这对直播功能至关重要),且缺少内存占用具体数据、完整页面代码及设备具体型号。代码示例展示了组件使用和onUnload中的资源释放尝试,但未展示context初始化过程,无法直接复现。复现步骤过于简略(仅"重复进入页面"),难以精确操作验证。
从知识库分析,该问题可能成立但需注意:1) 知识库明确指出App平台使用live-pusher需勾选manifest.json中的LivePusher模块;2) 对于直播场景,强烈推荐使用nvue页面,因其无层级问题且性能更优;3) 用户HBuilderX版本4.76高于3.4.1,理论上支持vue页面live-pusher,但资源释放需通过uni.createLivePusherContext正确调用stop/stopPreview。用户代码中直接操作this.context可能未按规范创建上下文,导致资源未释放。建议用户:1) 确认是否使用nvue;2) 按文档使用uni.createLivePusherContext获取context;3) 在onUnload中确保先隐藏组件再调用context.stop();4) 升级至最新HBuilderX验证。若仍存在内存问题,需提供更完整的nvue实现代码和内存监测数据进一步排查。 内容为 AI 生成,仅供参考

更多关于uni-app live-pusher组件在安卓APP中关闭页面不会释放内存的实战教程也可以访问 https://www.itying.com/category-93-b0.html


使用的nvue页面开发

// 封装“关闭并销毁live-pusher”的方法 async destroyLivePusher() { if (!this.context) return;
// 1. 先停止推流(若在推流)
if (this.isPushing) {
await new Promise(resolve => {
this.context.stopPush({
success: resolve,
fail: resolve // 失败也继续后续操作
});
});
}

      // 2. 停止预览(若在预览)  
      await new Promise(resolve => {  
        this.context.stopPreview({  
          success: resolve,  
          fail: resolve  
        });  
      });  

      if (this.$refs.livePusher) {  
        // 调用live-pusher组件的原生释放方法(uniapp nvue中部分版本支持)  
        this.$refs.livePusher.releaseHardware();  
      }  

      // 5. 销毁live-pusher上下文(解除原生实例引用)  
      if (this.context.destroy) {  
        this.context.destroy(); // 部分版本的上下文有destroy方法  
      }  

      // 6. 置空所有引用,让JS GC回收  
      this.context = null;  
      this.showLive = false;  
    },  

解决办法

这是一个已知的uni-app live-pusher组件在Android平台的内存管理问题。根据你的代码和描述,问题确实存在,主要原因是live-pusher组件的底层资源在页面销毁时没有完全释放。

从你的代码分析,虽然你在onUnload中尝试了多种释放方式,但有几个关键点需要注意:

  1. live-pusher的销毁时机问题:在nvue页面中,live-pusher组件即使通过v-if移除,其底层资源(摄像头、编码器等)可能仍然驻留。这是Android平台特有的问题。

  2. 建议的解决方案

onUnload() {
  // 先停止所有推流相关操作
  if (this.context) {
    try {
      this.context.stop();
      this.context.stopPreview();
      this.context.pause();
    } catch(e) {
      console.log('停止操作失败:', e);
    }
  }
  
  // 强制销毁live-pusher实例
  this.showLive = false;
  
  // 添加延迟确保组件销毁
  setTimeout(() => {
    // 清理相关引用
    this.context = null;
    this.$refs.livePusher = null;
  }, 100);
}
回到顶部