开发者

Java garbage collector [duplicate]

开发者 https://www.devze.com 2023-03-07 04:45 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Java garbage collector - When does it collect?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Java garbage collector - When does it collect?

When people say that garbage collector in Java is slowing the whole application down, what do they really mean by it?开发者_C百科 Isn't garbage collector only gets triggered whenever an Object is relieved? Please help me understand. Thank's!


Take a look at http://developers.sun.com/mobility/midp/articles/garbage/

As Java technology becomes more and more pervasive in the telecommunications (telco) industry, understanding the behavior of the garbage collector becomes more important. Typically, telco applications are near-real-time applications. Delays measured in milliseconds are not usually a problem, but delays of hundreds of milliseconds, let alone seconds, can spell trouble for applications of this kind. Quite simply, sub-optimal performance translates directly into loss of revenue.

Basically, if the garbage collector compacts a small heap then the delay is short, but if you have been generating a lot of garbage, and have a large reachable object graph, you can see large delays. New garbage collection algorithms (generational / incremental GC) have mitigated this to some degree but tuning GC is still somewhat a black art.


Garbage collection doesn't happen in specific moments. It happens when the VM decides to do so (probably when it runs out of memory).

Garbage collection is another work that your process should do, so in the total time, your program is slower. BUT during the execution the lag felt (by the user) is minimal.


When the Java Virtual Machine runs out of memory (or decides to start garbage collection for some other reason), the garbage collector will determine if there are any objects which are no longer in use. This is expensive (especially if you have a lot of objects), and can occur at unpredictable times.

In contrast, languages like C which do not have garbage collectors allow the programming a finer level of control. This can avoid expensive and unpredictable garbage collection runs, or at least ensure that they don't occur at inopportune moments.


In Java, object life is managed automatically. The Java garbage collector will perform mark and sweep algorithm (O(N) algorithm) to walk the object graph and clean up objects that are not being referenced. Users do not have a reliable way to trigger the garbage collection (user can call System.gc(), but it's not guaranteed to run immediately). When garbage collection will happen pretty much relies on the JVM implementation.

So, back to your question, when people say garbage collection is slowing their application down, it means the mark and sweep algorithm is taking away some of their machine's silicon time.

In iOS world, what you said is right, object gets garbage collected when they're relieved. Relieved here means the reference count of the object becomes 0, and the programmer is responsible for increasing or decreasing the reference count. In this case, the run time of garbage collection is O(1).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号