文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>[Java] Memory Leak in Java

[Java] Memory Leak in Java

时间:2010-05-21  来源:web_surf

  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. ...
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载