Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2108449
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: Java

2010-05-21 15:28:22

  1. Reference
    http://blog.chinaunix.net/u/9577/showart.php?id=2202557
  2. Policy
    When assign an object to a variable, then a reference to the object is created, if the reference is not destroyed, the object can not be released by Garbage Collector. ie.
    SomeClass a = new SomeClass();
    ....
    static SomeClass b = a;
    ....
    if 'b = null' or 'b = other_obj' is not called, the object can't be released.
  3. Java Garbage Collector will release object out of its scope
    1. Local Objects
      void fun()
      {
          SomeClass obj1 = new SomeClass();
          ....
          if (true)
          {
             SomeClass obj2 = new SomeClass();
          } //obj2 will be released automatically at the end of 'if' block without calling obj2 = null explicitly
          ....
      } //obj1 will be released automatically at the end of function without calling obj21= null explicitly
    2. Class Fields
      class MyClass
      {
          private SomeClass m_obj;
          public MyClass()
          {
              m_obj = new SomeClass();
          }
      }

      MyClass obj = new MyClass();
      ....
      obj = null;  //The m_obj of MyClass will be released here. you need not to release it with 'm_obj = null' in finalize() method of MyClass.
  4. Common Memory Leak in Java
    1. Unknown or unwanted object references
      These objects are no longer needed, but the garbage collector can not reclaim the memory because another object still refers to it.
    2. Long-living (static) objects
      These objects stay in the memory for the application's full lifetime. Objects tagged to the session may also have the same lifetime as the session, which is created per user and remains until the user logs out of the application.
    3. Failure to clean up or free native system resources
      Native system resources are resources allocated by a function external to Java, typically native code written in C or C++. Java Native Interface (JNI) APIs are used to embed native libraries/code into Java code.
    4. Bugs in the JDK or third-party libraries
      Bugs in various versions of the JDK or in the Abstract Window Toolkit and Swing packages can cause memory leaks.
  5. ...
阅读(1122) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~