简体   繁体   English

与Garbage Collector和finalize()方法相关的问题

[英]Questions related to Garbage Collector and finalize() method

I was reading about Garbage Collection and finalize() method of Java and there are some of the doubts that caught my mind. 我正在阅读Java的垃圾收集和finalize()方法,并且有一些疑点引起了我的注意。 Sorry if you think that these doubts are really silly. 对不起,如果你认为这些疑惑真的很傻。

  1. I was reading the article http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html . 我正在阅读文章http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html In this, point 5 says: 'Before removing an object from memory Garbage collection thread invokes finalize() method of that object and gives an opportunity to perform any sort of cleanup required'. 在这一点上,第5点说: '在从内存中删除对象之前,垃圾收集线程调用该对象的finalize()方法,并提供执行任何所需清理的机会'。 So does this thing happens for sure? 那么这件事肯定会发生吗? I mean will the finalize() method be always called before execution of Garbage Collector method? 我的意思是在执行垃圾收集器方法之前总是调用finalize()方法吗?

  2. How does the Garbage Collector knows that it needs to execute? 垃圾收集器如何知道它需要执行? For example, I have an application deployed on server, so when does the GC executes? 例如,我在服务器上部署了一个应用程序,那么GC什么时候执行? Does it executes periodically, or when some (say 1MB) amount of garbage is collected and a trigger is executed or something or it is just random and there's no way to determine when will it execute? 它是周期性地执行,还是当收集了一些(比如说1MB)的垃圾并且执行了一个触发器或某个或它只是随机的时候,并且无法确定它何时执行?

  3. How does it degrade the performance of my application since the garbage collection isn't happening? 由于垃圾收集没有发生,它如何降低我的应用程序的性能?

  4. Suppose I have a lots of garbage in my heap but the garbage collector isn't executed. 假设我的堆中有很多垃圾,但垃圾收集器没有被执行。 If this happens, so isn't it a bad behavior or a flaw of JVM? 如果发生这种情况,那么这不是一个坏行为或JVM的缺陷吗?

  5. Can we say that garbage collection which is done manually in C/C++ better than in Java taking the consideration that we as a programmer are smart enough and know when we need to dispose off the 'not-so-referenced' pointers? 我们可以说在C / C ++中手动完成的垃圾收集比在Java中更好地考虑到我们作为程序员足够聪明并知道什么时候需要处理掉“不那么引用”的指针吗?

  1. The finalize() method will always be called before the object is garbage collected. 在对象被垃圾回收之前,将始终调用finalize()方法。 There is no "garbage collector method". 没有“垃圾收集器方法”。

  2. The garbage collector will run whenever it feels like it. 垃圾收集器会在它感觉到的时候运行。 As a programmer you can't control it. 作为程序员,你无法控制它。 Technically you can, by making lots of garbage or by calling System.gc(), but you don't need to. 从技术上讲,你可以通过制作大量垃圾或调用System.gc(),但你不需要。

  3. The garbage collector will run whenever it needs to. 垃圾收集器将在需要时运行。 As a programmer you shouldn't need to be concerned about garbage collection performance, but there are some command-line options to tweak the behaviour of the garbage collector. 作为程序员,您不需要关心垃圾收集性能,但是有一些命令行选项可以调整垃圾收集器的行为。

  4. If you run out of memory and the garbage collector does not run, that's a JVM flaw. 如果内存不足并且垃圾收集器没有运行,那就是JVM漏洞。 It will run, though. 但它会运行。 The garbage collector will run whenever it needs to and whenever it's efficient. 垃圾收集器将在需要时以及何时高效运行。 If it's more efficient to let the heap fill up with garbage before collecting any, then it will do that. 如果在收集任何内容之前让堆充满垃圾更有效,那么它就会这样做。

  5. This is a very subjective question and there are good reasons behind both sides. 这是一个非常主观的问题,双方都有充分的理由。 Java's garbage collection reduces programming errors, but C and C++'s manual memory management provides more consistent performance and memory usage. Java的垃圾收集减少了编程错误,但C和C ++的手动内存管理提供了更一致的性能和内存使用。

  1. finalize() is supposed to happen right before the object is cleaned up. finalize()应该在清理对象之前发生。 The problem is - you don't know when it's gonna be (and if it will even ever happen). 问题是 - 你不知道它什么时候会发生(如果它会发生的话)。 Don't count on finalize to clean up, just do the cleaning yourself before the object loses its scope. 不要指望finalize清理,只是在对象失去其范围之前自己进行清洁。 Also, notice that finalize() runs on a garbage collection cycle. 另请注意, finalize()在垃圾回收周期上运行。 This means that if you implement finalize your garbaged object will take 2 cycles to clean up which means a waste of memory that can be cleaned. 这意味着如果你实现了finalize你的被动对象需要2个周期来清理,这意味着浪费了可以清理的内存。 And also more CPU that will be consumed to run finalize and you won't know when. 还有更多的CPU将用于运行finalize ,你不知道什么时候。 If you have lots of objects that have finalize then they might run together during GC which might pause your entire application for a long period of time . 如果你有很多已经finalize的对象,那么它们可能会在GC期间一起运行,这可能会暂停整个应用程序很长一段时间

  2. There are different algorithms that you can tune to decide when the garbage collection happens. 您可以调整不同的算法来确定垃圾收集何时发生。 Also, it depends on the heap size you allocate to your JVM (along with other parameters such as old vs new generation sizes, survivor space size and so on...). 此外,它还取决于您分配给JVM的堆大小(以及其他参数,例如旧的与新生成大小,幸存者空间大小等等)。 If you're dealing with small apps -usually the default configuration will be enough for you. 如果您正在处理小型应用程序 - 通常默认配置就足够了。 If you're writing large apps and need to consider performance over time, CPU time, and pause times (sometimes the GC pauses the entire app) - then you better read about the heap memory model and the different GC algorithms and parameters you can tune to control the GC to better suite your needs. 如果您正在编写大型应用程序并且需要考虑一段时间内的性能,CPU时间和暂停时间(有时GC会暂停整个应用程序) - 那么您最好阅读堆内存模型以及可以调整的不同GC算法和参数控制GC以更好地满足您的需求。

  3. Your application performance is affected by the GC cycles that happen (not GC cycles that don't happen). 您的应用程序性能会受到发生的GC循环的影响(不会发生GC循环)。 For example - if your heap is too small, you'll have frequent cycles. 例如 - 如果您的堆太小,您将经常进行循环。 If your heap is too big you'll have less frequent cycles but each of them might take longer to execute. 如果你的堆太大,你的循环次数会减少,但每次循环可能需要更长的时间来执行。 Again, that's all depend on the GC algorithms and tuning parameters you choose as well as the heap memory allocation (how many memory for young generation, how many for old generation, survivor-space ratio and so on...) 同样,这完全取决于您选择的GC算法和调整参数以及堆内存分配(年轻代有多少内存,老代有多少,幸存者空间比等等......)

  4. I don't see a scenario in which you have a lot of garbage and there's no collection, unless there's still enough space for allocating new objects. 除非仍有足够的空间来分配新对象,否则我没有看到你有大量垃圾并且没有收集的情况。 When the space runs out, or about to run out (depending on the collection algorithm you chose and the heap size and other params) there will be a collection. 当空间用完或即将耗尽时(取决于您选择的收集算法以及堆大小和其他参数),将有一个集合。

  5. Java garbage collector solves a painful problem that programmers had experienced in the non-managed languages such as C or C++ . Java垃圾收集器解决了程序员在非托管语言(如CC++遇到的痛苦问题。 It has its advantages and disadvantages. 它有其优点和缺点。 When you need absolute control on your memory, you'll use C . 当你需要绝对控制你的记忆时,你将使用C When you want to write systems more easily, and you don't mind allocating some extra memory for the GC, and to share a bit of CPU time with the GC, then you can write faster (and also more robust - because there are less memory leaks and other nasty stuff) with java. 如果您想更轻松地编写系统,并且不介意为GC分配一些额外的内存,并且与GC共享一些CPU时间,那么您可以更快地编写(也更强大 - 因为更少内存泄漏和其他令人讨厌的东西)用java。

[1] The finalize call is part of the garbage collection. [1] finalize调用是垃圾收集的一部分。

[2] The strategy and invocation of the garbage collector is a detail of the implementation. [2]垃圾收集器的策略和调用是实现的细节。 Hence no answer here. 因此这里没有答案。

[3,4] If you have a lot of tiny garbage, the collector might not perform garbage collection. [3,4]如果你有很多小垃圾,收藏家可能不会进行垃圾收集。 This hower might waste memory (I consider it a memory leak), if the 'tiny' objects hold memory the collector is not aware of (An image might be represented by a tiny handle/pointer to native memory). 如果“微小”对象保存收集器不知道的内存(图像可能由一个微小的句柄/指向本机内存的指针),这可能会浪费内存(我认为它是内存泄漏)。

[5] The concept of C++ is very different (omitting C here). [5] C ++的概念非常不同(这里省略C)。 A destructor can perform at a determite point to release resources allocated (See: RAII). 析构函数可以在确定点执行以释放分配的资源(参见:RAII)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM