一个函数的返回值不做处理,会引起内存泄露吗?

ameyume
博客专家认证
2018-12-07 10:46:04
822        private boolean processImageFile(String path) {
823 try {
824 mBitmapOptions.outWidth = 0;
825 mBitmapOptions.outHeight = 0;
826 BitmapFactory.decodeFile(path, mBitmapOptions);
827 mWidth = mBitmapOptions.outWidth;
828 mHeight = mBitmapOptions.outHeight;
829 return mWidth > 0 && mHeight > 0;
830 } catch (Throwable th) {
831 // ignore;
832 }
833 return false;
834 }

http://androidxref.com/9.0.0_r3/xref/frameworks/base/media/java/android/media/MediaScanner.java#822

479    public static Bitmap decodeFile(String pathName, Options opts) {
480 validate(opts);
481 Bitmap bm = null;
482 InputStream stream = null;
483 try {
484 stream = new FileInputStream(pathName);
485 bm = decodeStream(stream, null, opts);
486 } catch (Exception e) {
487 /* do nothing.
488 If the exception happened on open, bm will be null.
489 */
490 Log.e("BitmapFactory", "Unable to decode stream: " + e);
491 } finally {
492 if (stream != null) {
493 try {
494 stream.close();
495 } catch (IOException e) {
496 // do nothing here
497 }
498 }
499 }
500 return bm;
501 }


如上代码,BitmapFactory.decodeFile会返回一个Bitmap对象,但在上面的代码中,没有对返回值做处理,会存在Bitmap内存泄露吗?
需要增加一个接收返回值Bitmap的处理,在finally中再执行recycle处理吗?
...全文
634 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ameyume 2018-12-10
  • 打赏
  • 举报
回复
另外,假设有创建Bitmap并返回,但没有处理返回值;这样会造成内存泄漏吗? 还是系统会自动回收没有引用的返回值?
王能 2018-12-10
  • 打赏
  • 举报
回复
这是java的基础知识了,没有被引用的对象都会被垃圾回收定时回收掉的,但是类似于bitmap这种很浪费内存的对象最好直接调用Recycler,因为垃圾回收不是实时的,堆积多了太浪费内存(不是内存泄漏。内存好比池子里的水,用一半和用1/3都不会干,但后面的人就用得少了就有可能卡顿或者用干导致挂断)
键盘舞者113 2018-12-07
  • 打赏
  • 举报
回复
不需要,现在bitmap能够自动回收,如果你担心回收问题,那就在当前activity onDestroy里回收Bitmap
王能 2018-12-07
  • 打赏
  • 举报
回复
并且inJustDecodeBounds也有设置
王能 2018-12-07
  • 打赏
  • 举报
回复
好吧,片段看不出来东西,源码里在其他地方有用到
ameyume 2018-12-07
  • 打赏
  • 举报
回复
inJustDecodeBounds added in API level 1
public boolean inJustDecodeBounds
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.
的确不会分配内存
ameyume 2018-12-07
  • 打赏
  • 举报
回复
引用 5 楼 王能 的回复:
我敢肯定,第一段代码肯定不是android,顶多是某第三方的代码

下面有个链接,可以打开看看:
http://androidxref.com/9.0.0_r3/xref/frameworks/base/media/java/android/media/MediaScanner.java#822
or
https://android.googlesource.com/platform/frameworks/base/+/master/media/java/android/media/MediaScanner.java#860
        private boolean processImageFile(String path) {
try {
mBitmapOptions.outWidth = 0;
mBitmapOptions.outHeight = 0;
BitmapFactory.decodeFile(path, mBitmapOptions);
mWidth = mBitmapOptions.outWidth;
mHeight = mBitmapOptions.outHeight;
return mWidth > 0 && mHeight > 0;
} catch (Throwable th) {
// ignore;
}
return false;
}

王能 2018-12-07
  • 打赏
  • 举报
回复
我敢肯定,第一段代码肯定不是android,顶多是某第三方的代码
ameyume 2018-12-07
  • 打赏
  • 举报
回复
谢谢各位,这个不是我写的,是Android 原生代码。
王能 2018-12-07
  • 打赏
  • 举报
回复
都声明成全局的会浪费很多内存,而且还不好看,建议贴主多看看java规范
王能 2018-12-07
  • 打赏
  • 举报
回复
你这样不会泄露,但会浪费内存,options也不需要声明这么远,建议这样写
BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;//设置true表示只加载轮廓
        BitmapFactory.decodeFile(srcPath, newOpts);//初始化newOpts
        int width = newOpts.outWidth;
        int height = newOpts.outHeight;

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧