Does 'new' cause a memory leak in Java?

This question already has an answer here:

  • Creating a memory leak with Java 52 answers

  • In short, no. Java has a built-in (and mandatory) garbage collector. C++ does not.

    Once an Object is no longer reachable (in Java), it is eligible for garbage collection (and the collector may free the memory).


    A "memory leak" in a memory-managed language like Java doesn't have exactly the same meaning as it does in C++ because Java doesn't require you to explicitly free memory that you've allocated.

    With that said, you can have basically the same effect through. Collection classes and implementations of the Observer Pattern tend to be a major culprit in this regard. One of the consequences of memory management is that if any object holds a reference to an object, the object will stay in memory whether you intend to use it again or not. This can result in objects staying around in memory much longer than necessary (perhaps even for the duration of the program).

    You can also run into problems if you refer to unmanaged objects. There are a number of possible solutions to this; for example, C# solves this with the Dispose Pattern. The fact is, though, introducing unmanaged object makes it possible that you will end up with an actual memory leak.

    链接地址: http://www.djcxy.com/p/19894.html

    上一篇: 创建一个内存泄漏给定一个接口符合

    下一篇: 'new'是否会导致Java中的内存泄漏?