在Java堆的年轻部分和终身部分中分配大数组 [英] Large Array allocation across young and tenured portions of java Heap

查看:123
本文介绍了在Java堆的年轻部分和终身部分中分配大数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我认为我简化了这个问题,因为这些区域有他们自己的分区域。我只是想了解一个单一的分配是否可以跨越多个地区(年轻和终身)。如果是,那么垃圾收集是如何完成的,因为所采用的策略对于不同地区是不同的(我认为至少在ParallelGC中不同的收集器用于不同的地区)。

为了使问题更清楚,下面是一个例子。可以说-Xms和-Xmx设置在900M,NewRatio(年轻到终身比例)是2.这将导致一个300M的年轻区域和600M的终端。现在,如果我尝试分配一个新字节[750],分配将通过给定的区域没有足够的连续空间。



我之所以问这个问题是我有一个问题(OutofMemoryError)与分配一个大型数组(这实际上是我的代码中的大部分分配),我通过更改-XX:NewRatio来解决它。我的假设是,大数组将确保到终身区域,并且我的终身区域至少应该具有与数组所需的空间一样多的空间。 解决方案

Java数组总是分配在连续的内存中。他们不会分手。



新的对象总是分配给年轻一代,然后如果他们在足够的GC事件中生存下来,就会转入终身职位。因此,请确保您的年轻分配足够大。



如果没有连续750个字节的区域,您将无法获取新字节[750] 。


Is it possible for a large array to be allocated across young and tenured areas of java heap?

I think i am simplifying the question a bit given that these areas have their own subregions. I am just trying to understand whether a single allocation can span across multiple regions (young and tenured). If yes then how is garbage collection done, given that the strategy used is different for different regions (I think atleast in case of ParallelGC different collectors are used for different regions).

Here is an example in order to make the question more clear. Lets say -Xms and -Xmx are set at 900M and NewRatio (ratio of young to tenured) is 2. This would lead to a young region of 300M and tenured of 600M. Now if I try to allocated a "new byte[750]" would the allocation go through given that none of the region might have enough contiguous space.

The reason I am asking this question is that I had an issue (OutofMemoryError) with allocating a large array (which was really the majority of allocation in my code) and I resolved it by changing the -XX:NewRatio. My assumption was that the large array will go to tenured region for sure and that my tenured region should at least have as much space as required for the array.

解决方案

Java array are always allocated in contiguous memory. They won't get split up.

New objects are always allocated in young generation, then moved into tenured if they survive enough GC events. So make sure your young allocation is large enough.

If there is no contiguous region of 750 bytes, you won't be able to get new byte[750] to work.

这篇关于在Java堆的年轻部分和终身部分中分配大数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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