如何防止Java中的碎片? [英] How to prevent fragmentation in Java?

查看:45
本文介绍了如何防止Java中的碎片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一次面试,我被问到如何防止 Java 中的内存碎片.谁能总结一下 Java 是如何处理的?我读过一些文件,但很复杂.

I have an interview and I was ask how to prevent Memory Fragmentation in Java. Can Anyone summarize how Java deal with it ? I've read some documents but it very complicated.

推荐答案

简短的回答是:您使用分代垃圾收集器.

The short answer is: you use a generational garbage collector.

除了大多数现代 Java 实现1默认使用分代收集器.换句话说,您(程序员)不需要处理问题,因为它已经处理了.

Except that most modern Java implementations1 use a generational collector by default. In other words, you (the programmer) don't need to deal with the problem because it is already dealt with.

您可能遇到碎片的唯一情况是,如果您对低暂停收集器进行了错误调整,并且它已经回退到执行停止世界"完整收集.那时,您可能正在使用非压缩收集器,并且可能会出现碎片.但解决方案是调整调整(或应用程序,或堆大小)以避免陷入这种情况.

The only situation where you might run into fragmentation is if you have mis-tuned the low-pause collector and it has fallen back to doing a "stop the world" full collection. At that point, you might be using a non-compacting collector, and fragmentation might ensue. But the solution there is to adjust the tuning (or the application, or the heap size) to avoid getting into that situation.

如果您想了解有关垃圾收集器工作的更多信息,请参阅有关该主题的完整教科书.并且在各种文章和已发表的研究论文中深入介绍了各种 Java 收集器.

If you want more information about garbage collectors work, there are whole textbooks on the subject. And the various Java collectors are covered in depth in various articles and published research papers.

我读过一些文档,但很复杂.

I've read some documents but it very complicated.

是的,是的.成为专家是一项艰苦的工作.

Yes, it is. It is hard work becoming an expert.

1 - 一些非常古老的 Java 实现使用原始标记清除收集器.但它们早已不复存在.

这篇关于如何防止Java中的碎片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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