Java中内存分配的典型速度是多少? [英] What is the typical speed of a memory allocation in Java?

查看:86
本文介绍了Java中内存分配的典型速度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分析Java应用程序,发现对象分配发生的速度比我预期的要慢得多.我运行了一个简单的基准来尝试确定小对象分配的总体速度,发现在我的机器上分配一个小对象(3个浮点的向量)似乎需要200纳秒.我在(双核)2.0 GHz处理器上运行,因此大约需要400个CPU周期.我想问一下这里的人,他们对Java应用程序进行了概要分析,然后才可以预期这种速度.对我来说似乎有点残酷和不寻常.毕竟,我认为像Java这样的语言可以压缩堆并重定位对象,其对象分配如下所示:

I was profiling a Java application and discovered that object allocations were happening considerably slower than I'd expect. I ran a simple benchmark to attempt to establish the overall speed of small-object allocations, and I found that allocating a small object (a vector of 3 floats) seems to take about 200 nanoseconds on my machine. I'm running on a (dual-core) 2.0 GHz processor, so this is roughly 400 CPU cycles. I wanted to ask people here who have profiled Java applications before whether that sort of speed is to be expected. It seems a little cruel and unusual to me. After all, I would think that a language like Java that can compact the heap and relocate objects would have object allocation look something like the following:

int obj_addr = heap_ptr;
heap_ptr += some_constant_size_of_object
return obj_addr;

..这是几行组装.至于垃圾收集,我没有分配或丢弃足够的对象来发挥作用.当我通过重用对象优化代码时,我获得的性能约为每个对象要处理的15纳秒,而不是每个需要处理的对象200 ns,因此重用对象可以极大地提高性能.我真的很想不重用对象,因为这样会使符号有点毛茸茸(许多方法需要接受receptacle自变量而不是返回值).

....which is a couple lines of assembly. As for garbage collection, I don't allocate or discard enough objects for that to come into play. When I optimize my code by re-using objects, I get performance on the order of 15 nanoseconds / object I need to process instead of 200 ns per object I need to process, so re-using objects hugely improves performance. I'd really like to not reuse objects because that makes notation kind of hairy (many methods need to accept a receptacle argument instead of returning a value).

问题是:对象分配花费这么长时间是正常的吗?还是机器上出现了问题,一旦修复,可能会使我在此问题上有更好的性能?小对象分配通常要花多长时间,并且有典型值吗?我正在使用客户端计算机,目前不使用任何编译标志.如果您的计算机上的运行速度更快,那么计算机的JVM版本和操作系统是什么?

So the question is: is it normal that object allocation is taking so long? Or might something be wrong on my machine that, once fixed, might allow me to have better performance on this? How long do small-object allocations typically take for others, and is there a typical value? I'm using a client machine and not using any compile flags at the moment. If things are faster on your machine, what is your machine's JVM version and operating system?

我意识到个人的表现可能会发生很大的变化,但是我只是想问一下我上面提到的数字是否似乎在正确的范围内.

I realize that individual mileage may vary greatly when it comes to performance, but I'm just asking whether the numbers I'm mentioning above seem like they're in the right ballpark.

推荐答案

当对象很小且没有GC成本时,创建对象的速度非常快.

Creating objects is very fast when the object is small and there is no GC cost.

final int batch = 1000 * 1000;

Double[] doubles = new Double[batch];
long start = System.nanoTime();

    for (int j = 0; j < batch; j++)
        doubles[j] = (double) j;

long time = System.nanoTime() - start;
System.out.printf("Average object allocation took %.1f ns.%n", (double) time/batch);

使用-verbosegc

Average object allocation took 13.0 ns.

注意:没有发生GC.但是,增加大小,该程序需要等待在GC中复制内存.

Note: no GCs occurred. However increase the size, and the program needs to wait to copy memory around in the GC.

final int batch = 10 *1000 * 1000;

打印

[GC 96704K->94774K(370496K), 0.0862160 secs]
[GC 191478K->187990K(467200K), 0.4135520 secs]
[Full GC 187990K->187974K(618048K), 0.2339020 secs]
Average object allocation took 78.6 ns.

我怀疑您的分配相对较慢,因为您正在执行GC.解决此问题的一种方法是增加应用程序可用的内存. (尽管这可能会延迟成本)

I suspect your allocation is relatively slow because you are performing GCs. One way around this is to increase the memory available to the application. (Though this may just delay the cost)

如果我再次使用-verbosegc -XX:NewSize=1g

Average object allocation took 9.1 ns.

这篇关于Java中内存分配的典型速度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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