有关垃圾收集和完成的问题()方法 [英] Questions related to Garbage Collector and finalize() method

查看:149
本文介绍了有关垃圾收集和完成的问题()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到有关垃圾回收和Java的finalize()方法,并有一些引起了我心中的疑惑。很抱歉,如果你认为这些怀疑是真的傻了。


  1. 我读的文章的http:// javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html 。在这方面,5点说:
    从内存垃圾收集线程删除对象之前完成调用该对象的()方法,并给出了执行所需的任何种类的清理的机会。
    那么,这事情发生的肯定吗?我的意思是将finalize()方法垃圾收集方法执行前总是叫什么名字?


  2. 如何进行垃圾收集器知道它需要执行?例如,我有部署在服务器上的应用程序,所以当在做GC执行时?它是否定期执行,或在一些(比如1MB)垃圾量被收集和执行触发器或某事,或者只是随机的,没有办法确定何时会执行?


  3. 它如何降低我的应用程序的性能,因为垃圾收集是不是发生了什么?


  4. 假设我有很多的垃圾在我的堆,但不执行垃圾收集器。如果发生这种情况,所以是不是一个不好的行为或JVM的缺陷?


  5. 我们能说,垃圾收集其在C手工完成/ C ++比Java好走的考虑,我们作为一个程序员足够聪明,知道什么时候,我们需要处理掉不那么引用指针?



解决方案

  1. 完成()应该对象清理之前发生的权利。现在的问题是 - 你不知道什么时候会是(如果甚至会不会发生的话)。不要在不能算完成来清理,只是做清洗自己的对象失去它的范围了。此外,请注意的finalize()上一个垃圾收集周期运行。这意味着,如果你执行完成您garbaged对象将需要2个周期来清理这意味着浪费内存,可以进行清洗。而且更多的CPU,这将消耗运行完成,你不知道什么时候。如果你有大量的对象已敲定那么他们可能会一起GC可能的暂停的很长一段时间你的整个应用过程中运行


  2. 有不同的算法,你可以调整,以决定何时垃圾收集情况。此外,它取决于你分配给你的JVM(以及其他参数如旧VS新一代的大小,生存空间大小等...)堆大小。如果你处理的小应用程序 - 通常默认的配置就足够你。如果你正在编写大型应用和需要考虑性能随时间,CPU时间和暂停时间(有时是GC暂停整个应用程序) - 那么你最好了解堆内存模型和不同的GC算法,您可以调整参数控制GC,以更好地适合您的需求。


  3. 您的应用程序的性能是由发生的(即没有发生,没有GC周期)的GC周期的影响。例如 - 如果你的堆太小,你就会有频繁的周期。如果你的堆过大,你就会有那么频繁周期,但他们每个人可能需要更长的时间来执行。同样,这一切都依赖于GC算法和调整参数选择以及堆内存分配(多少内存年轻一代,有多少老一代,幸存者空间的比率等等...)


  4. 我不明白在你有大量的垃圾,有没有收藏,除非仍有分配新对象足够空间的情况下。当空间用完或者即将用完(取决于您选择的收集算法和堆的大小和其他PARAMS)会有一个集合。


  5. Java垃圾收集器解决了程序员在非托管语言曾经历过如℃的痛苦的问题 C ++ 。它有它的优点和缺点。当你需要在你的记忆绝对的控制权,你会使用 C 。当你想更轻松地编写系统,并且你不介意分配的GC一些额外的内存,并与GC分享一点的CPU时间,那么你可以写更快(也更强大的 - 因为有少内存泄漏和其他讨厌的东西)用java。


I was reading about Garbage Collection and finalize() method of Java and there are some of the doubts that caught my mind. 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. 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'. So does this thing happens for sure? I mean will the finalize() method be always called before execution of Garbage Collector method?

  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? 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?

  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?

  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?

解决方案

  1. finalize() is supposed to happen right before the object is cleaned up. 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. Also, notice that finalize() runs on a garbage collection cycle. 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. And also more CPU that will be consumed to run finalize and you won't know when. 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.

  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...). 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.

  3. Your application performance is affected by the GC cycles that happen (not GC cycles that don't happen). 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...)

  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++. It has its advantages and disadvantages. When you need absolute control on your memory, you'll use 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.

这篇关于有关垃圾收集和完成的问题()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆