Android开发笔记——常见BUG类型之内存泄露与线程安全

发布时间:2020-06-13 16:19:15 作者:xsster
来源:网络 阅读:619

一、内存泄露

1、很抱歉,”XXX”已停止运行。OOM?

Android开发笔记——常见BUG类型之内存泄露与线程安全

怎样才能让app报OOM呢?最简单的办法如下:

Bitmap bt1 = BitmapFactory.decodeResource(this.getResources(), R.drawable.p_w_picpath);
Bitmap bt2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.p_w_picpath);
Bitmap btn = ...
 
2、查看内存占用

Android开发笔记——常见BUG类型之内存泄露与线程安全

Android开发笔记——常见BUG类型之内存泄露与线程安全

 
3、发生内存泄露的条件

首先,每个app有最大内存限制。

ActivityManager activityManager = (ActivityManager) context.getSystemServiceContext.ACTIVITY_SERVICE);
activityManager.getMemoryClass();
 
getMemoryClass()取到的是最大内存资源。Android中的堆内存分为Native Heap和Dalvik Heap。C/C++申请的内存空间在Native Heap中,Java申请的内存空间则在Dalvik Heap中。对于head堆的大小限制,可以查看/system/build.prop文件:
 
dalvik.vm.heapstartsize=8m
dalvik.vm.heapgrowthlimit=96m
dalvik.vm.heapsize=256m
注意:

heapsize参数表示单个进程heap可用的最大内存,但如果存在以下参数”dalvik.vm.headgrowthlimit =96m”表示单个进程heap内存被限定在96m,即程序运行过程实际只能使用96m内存。

如果申请的内存资源超过上述限制,系统就会抛出OOM错误。

 

4、常见避免OOM的措施

以下主要从四个方面总结常见的措施:1)减小对象的内存占用;2)内存对象的重复利用;3)避免对象的内存泄露;4)内存使用策略优化。

4.1 减小对象的内存占用

4.2 内存对象的重复利用

4.3 避免对象的内存泄露

4.1和4.2都是比较常规的措施,4.3需要重点关注。

1)Activity泄露

导致Activity泄露的原因较多,下面列举一些比较常见的。从原理上主要分为两类:i)静态对象;ii)this$0

下面是通过MAT分析一个Activity泄露的截图:
 Android开发笔记——常见BUG类型之内存泄露与线程安全

2)考虑使用Application Context而不是Activity Context

对于大部分非必须使用Activity Context的情况(Dialog的Context就必须是Activity Context),我们都可以考虑使用Application Context而不是Activity的Context,这样可以避免不经意的Activity泄露。

3)注意临时Bitmap对象的及时回收

虽然在大多数情况下,我们会对Bitmap增加缓存机制,但是在某些时候,部分Bitmap是需要及时回收的。例如临时创建的某个相对比较大的bitmap对象,在经过变换得到新的bitmap对象之后,应该尽快回收原始的bitmap,这样能够更快释放原始bitmap所占用的空间。

4)内存占用监控
通过Runtime获取maxMemory,而maxMemory-totalMemory即为剩余可使用的dalvik内存。定期检查这个值,达到80%就去释放各种cache资源(bitmap的cache)。

Android开发笔记——常见BUG类型之内存泄露与线程安全

/**
 * Returns the maximum number of bytes the heap can expand to. See {@link #totalMemory} for the
 * current number of bytes taken by the heap, and {@link #freeMemory} for the current number of
 * those bytes actually used by live objects. */int maxMemory = Runtime.getRuntime().maxMemory()); // 应用程序最大可用内存/**
 * Returns the number of bytes taken by the heap at its current size. The heap may expand or
 * contract over time, as the number of live objects increases or decreases. See
 * {@link #maxMemory} for the maximum heap size, and {@link #freeMemory} for an idea of how much
 * the heap could currently contract. */long totalMemory = Runtime.getRuntime().totalMemory()); // 应用程序已获得内存/**
 * Returns the number of bytes currently available on the heap without expanding the heap. See
 * {@link #totalMemory} for the heap's current size. When these bytes are exhausted, the heap
 * may expand. See {@link #maxMemory} for that limit. */long freeMemory = Runtime.getRuntime().freeMemory()); // 应用程序已获得内存中未使用内存

Android开发笔记——常见BUG类型之内存泄露与线程安全


5)注意Cursor对象是否及时关闭

在程序中我们经常会进行查询数据库的操作,但时常会存在不小心使用Cursor之后没有及时关闭的情况。这些Cursor的泄露,反复多次出现的话会对内存管理产生很大的负面影响,我们需要谨记对Cursor对象的及时关闭。

4.4 内存使用策略优化

二、线程安全

1、下面的方法是线程安全的吗?
怎样使上述方法线程安全?
 
2、Java中的线程安全

怎样保持在多线程环境下的数据一致性,Java提供了多种方法实现:

  1. synchronized

  2. java.util.concurrent.atomic

  3. java.util.concurrent.locks

  4. thread safe collection(ConcurrentHashMap)

  5. volatile

2.1 synchronized

JVM保证被synchronized关键字修饰的代码段在同一时间只能被一个线程访问,内部通过对对象加锁来实现的。当方法被synchronized修饰时,锁加在对象上;当方法同时为static时,锁加在类上。从性能的角度来讲,一般不建议直接将锁加在类上,这样会使得类的所有对象的该方法均为synchronized的。

从之前扫描的问题来看,在编写synchronized程序时主要有两点需要注意:

1) 锁加在哪里?

Android开发笔记——常见BUG类型之内存泄露与线程安全

List<ResultPoint> currentPossible = possibleResultPoints;
List<ResultPoint> currentLast = lastPossibleResultPoints;int frameLeft = frame.left;int frameTop = frame.top;if (currentPossible.isEmpty()) {
    lastPossibleResultPoints = null;
} else {
    possibleResultPoints = new ArrayList<>(5);
    lastPossibleResultPoints = currentPossible;
    paint.setAlpha(CURRENT_POINT_OPACITY);
    paint.setColor(resultPointColor);    synchronized (currentPossible) {        for (ResultPoint point : currentPossible) {
            canvas.drawCircle(frameLeft                    + (int) (point.getX() * scaleX), frameTop                    + (int) (point.getY() * scaleY), POINT_SIZE,
                    paint);
        }
    }
}

Android开发笔记——常见BUG类型之内存泄露与线程安全

上述方法中,possibleResultPoints的创建没有采用同步措施,需要使用Collections.synchronizedXxx

List<MyType> list = Collections.synchronizedList(new ArrayList(<MyType>));
...synchronized(list){    for(MyType m : list){
        foo(m);
        m.doSomething();
    }
}
一般比较推荐创建一个虚拟的对象专门用于获取锁。

Android开发笔记——常见BUG类型之内存泄露与线程安全

//dummy object variable for synchronizationprivate Object mutex=new Object();
...//using synchronized block to read, increment and update count value synchronouslysynchronized (mutex) {
        count++;
}

Android开发笔记——常见BUG类型之内存泄露与线程安全


PS:直接在方法上加synchronized可能DoS***喔,举个栗子:

Android开发笔记——常见BUG类型之内存泄露与线程安全

public class MyObject {    // Locks on the object's monitor
    public synchronized void doSomething() { 
    // ...    }
}// ***的代码MyObject myObject = new MyObject();synchronized (myObject) {    while (true) {        // Indefinitely delay myObject        Thread.sleep(Integer.MAX_VALUE); 
    }
}

Android开发笔记——常见BUG类型之内存泄露与线程安全

***的代码获取了MyObject对象的锁,导致doSomething死锁,从而引发Denial of Service。

Android开发笔记——常见BUG类型之内存泄露与线程安全

public class MyObject {    //locks on the class object's monitor
    public static synchronized void doSomething() { 
    // ...    }
}// ***的代码synchronized (MyObject.class) {    while (true) {
        Thread.sleep(Integer.MAX_VALUE); // Indefinitely delay MyObject    }
}

Android开发笔记——常见BUG类型之内存泄露与线程安全

2) 死锁。

Android开发笔记——常见BUG类型之内存泄露与线程安全

public class ThreadDeadlock {    public static void main(String[] args) throws InterruptedException {
        Object obj1 = new Object();
        Object obj2 = new Object();
        Object obj3 = new Object();
        Thread t1 = new Thread(new SyncThread(obj1, obj2), "t1");
        Thread t2 = new Thread(new SyncThread(obj2, obj3), "t2");
        Thread t3 = new Thread(new SyncThread(obj3, obj1), "t3");
        t1.start();
        Thread.sleep(5000);
        t2.start();
        Thread.sleep(5000);
        t3.start();
    }
}class SyncThread implements Runnable{    private Object obj1;    private Object obj2;    public SyncThread(Object o1, Object o2){        this.obj1=o1;        this.obj2=o2;
    }
    @Override    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name + " acquiring lock on "+obj1);        synchronized (obj1) {
         System.out.println(name + " acquired lock on "+obj1);
         work();
         System.out.println(name + " acquiring lock on "+obj2);         synchronized (obj2) {
            System.out.println(name + " acquired lock on "+obj2);
            work();
        }
         System.out.println(name + " released lock on "+obj2);
        }
        System.out.println(name + " released lock on "+obj1);
        System.out.println(name + " finished execution.");
    }    private void work() {        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Android开发笔记——常见BUG类型之内存泄露与线程安全


推荐阅读:
  1. 操作系统笔记之进程与线程
  2. Android开发中应该避免的内存泄露

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

开发 android roi

上一篇:Android使用zxing(二维码工具类)类库的导入方式

下一篇:43.C#--写一个长度为10的集合,要求里面随机存放10个数字0-9,要求里面所有数字不能重

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》