作家
登录

Android面试被问到内存泄漏了杂整?

作者: 来源: 2017-12-19 09:15:40 阅读 我要评论

  •  /** 
  •  
  •      * 获取全局的context 
  •  
  •      * @return 返回全局context对象 
  •  
  •      */ 
  •  
  •     public static Context getContext(){         
  •  
  •         return context; 
  •  
  •     }  
  •  
  •  
  • public class SingleInstanceClass {     
  •  
  •     private static SingleInstanceClass instance;     
  •  
  •     private Context mContext;     
  •  
  •     private SingleInstanceClass() { 
  •  
  •        mContext=MyApplication.getContext; 
  •  
  •     }     
  •  
  •     public SingleInstanceClass getInstance() {         
  •  
  •         if (instance == null) { 
  •  
  •             instance = new SingleInstanceClass(); 
  •  
  •         }         
  •  
  •         return instance; 
  •  
  •     } 
  •  
  •  
  •   1. 引用计数法:对象内部定义引用变量,当该对象被某个引用变量引用时则计数加1,当对象的某个引用变量超出身命周期或者引用了新的变量时,计数减1。任何引用计数为0的对象实例都可以被GC。这种算法的长处是:引用计数收集器可以很快的履行,交错在法度榜样运行中。对法度榜样须要不被长时光打断的及时情况比较有利。缺点:无法检测出轮回引用。

    1. public class TestActivity extends AppCompatActivity {     
    2.     public static InnerClass innerClass = null;  
    3.     
    4.     @Override 
    5.     protected void onCreate(@Nullable Bundle savedInstanceState) {         
    6.         super.onCreate(savedInstanceState);         
    7.         if (innerClass == null
    8.             innerClass = new InnerClass(); 
    9.     }     
    10.     private class InnerClass {         

        推荐阅读

        一周云势:合纵连横竞争加剧,天上地下欢喜冤家

      此次的成功融资让金山云的用户也跟着受益,因为金山云的主流产品将周全降价,个中CDN、云数据库Redis新价动人,降幅最大年夜可达到50%、60%;云办事器价格最大年夜降幅跨越30%;对象存储产品>>>详细阅读


      本文标题:Android面试被问到内存泄漏了杂整?

      地址:http://www.17bianji.com/lsqh/39898.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)