Home  >  Q&A  >  body text

既然 Java 的垃圾回收机制能够自动的回收内存,怎么还会出现内存泄漏的情况呢

既然 Java 的垃圾回收机制能够自动的回收内存,怎么还会出现内存泄漏的情况呢

伊谢尔伦伊谢尔伦2744 days ago787

reply all(10)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:51:43

    Memory resources are limited. Garbage collection only recycles "garbage". Objects that are useful for running your program will not be recycled.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:51:43

    There are two situations of memory overflow: one is stack overflow, such as calling an infinite recursion. There is also a heap overflow, that is, the object that comes out of new is not destroyed immediately, such as new all the time.

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:51:43

    Because some poorly written programs will create objects infinitely before the GC mechanism takes effect.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:51:43

    Generally speaking, memory leaks in Java refer to the fact that allocated memory that is no longer needed by the program cannot be reclaimed.
    The garbage collection mechanism determines whether the memory can be recycled through the reachability of the object and the Root object. However, due to programming errors or other reasons, expired object references are still held, and the garbage collector cannot reclaim the relevant space.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:51:43

    There is nothing he can do if he occupies the manhole and does not poop.
    I recommend reading the blog written by my colleague: http://blog.csdn.net/zhanggan...

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:51:43

    Some objects are actually no longer needed but are always referenced and cannot be recycled

    reply
    0
  • PHPz

    PHPz2017-04-18 10:51:43

    For now, things made by humans cannot surpass humans themselves. Java’s GC algorithm was written by humans. However, some people will risk their lives and write code that cannot be recycled by GC

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:51:43

    This is just like 中国那么大,为什么还有人在朝阳区懵逼.

    reply
    0
  • PHPz

    PHPz2017-04-18 10:51:43

    My own understanding, please correct me if I am wrong.
    In JDK6, a method of String is called subString, which is used to generate a substring. For faster generation, String has a constructor,
    String(int offset, int count, char value[]) {

    this.value = value;
    this.offset = offset;
    this.count = count;

    }
    directly points to the original String array. We all know that a new String string in the constant pool will be generated every time. However, this reference causes the original String to not be recycled. Because the value of subString will point to him. This will lead to memory leaks.

    The JVM does perform GC by itself without too much interference from programmers. However, due to some incorrect operations, some objects that no longer need to be used still have references, which leads to memory leaks.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:51:43

    JVM: Programmers hold on to garbage, which makes me very embarrassed.

    reply
    0
  • Cancelreply