Java中对象创建需要多少个cpu周期? [英] How many cpu cycles an object creation takes in java?

查看:410
本文介绍了Java中对象创建需要多少个cpu周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇创建一个在Java中没有字段的类的对象需要多少个cpu周期或时间?我正在与同事讨论是否要以独特的方式创建新对象来还原某些东西或创建uuid是个好主意,在他的辩护中,他说现在用Java创建对象的重量很轻,我也同意. >

但是问题是,它如何与uuid生成等完全不同的东西进行比较?

因此,怀疑分配对象需要多少cpu周期&具体步骤是什么?

解决方案

在Java中创建对象可能需要花费零到十亿个周期.

  • 零:当对象没有逃脱局部作用域时,由于分配消除优化,JIT编译器可能会用局部变量替换分配.
  • 数十亿美元,因为任何分配都可能触发(可能很长)垃圾回收.

我已经在答案.

在TLAB中分配对象的最常见方法包括以下步骤:

  1. 加载tlab_top指针(通常在x64上有一个专门用于线程本地化的CPU寄存器).
  2. 将其增加一个对象的大小.
  3. tlab_top + object_sizetlab_end进行比较.如果需要,请跳转到慢速路径.
  4. 存储新值tlab_top.前一个值是新创建的对象的地址.
  5. 设置对象的默认标题.
  6. 设置klass字段-指向对象的类元数据的指针.
  7. 用零初始化其余对象数据.甚至没有字段的对象也可能具有对齐填充.

这些全部约有10-15条CPU指令.

让我们使用 JMH 测量对象创建的平均时间. a>基准.

package bench;

import org.openjdk.jmh.annotations.Benchmark;

public class Alloc {

    @Benchmark
    public Object baseline() {
        return "Some preallocated object";
    }

    @Benchmark
    public Object newObject() {
        return new Object();
    }
}

结果:

Benchmark        Mode  Cnt  Score   Error  Units
Alloc.baseline   avgt   10  3,428 ± 0,089  ns/op
Alloc.newObject  avgt   10  4,505 ± 0,056  ns/op

因此,在2.4GHz CPU上,对象分配以及基准测试开销大约需要4.5 ns或大约11个周期.与UUID生成算法相比,这确实是便宜的.

I am curious how many cpu cycle or time does it takes to create an object of class that has no field in java? I was disscussing it with colleague whether it would be good idea to create new object for unique way to reffer something or create uuid, in his defence he said that creating object is very light weight in java these days, which I also agree.

But question is how can it compare to something completely different like uuid generation?

Hence, the doubt how much cpu cycles does it takes to allocate object & what are exact steps involved?

解决方案

Object creation in Java may take anywhere from zero to billions cycles.

  • Zero: when an object does not escape a local scope, JIT compiler may replace an allocation with local variables as a result of the allocation elimination optimization.
  • Billions, because any allocation may trigger (potentially long) garbage collection.

I've already given a high-level overview of allocation in HotSpot JVM in this and this answer.

The most common way to allocate an object in TLAB involves the following steps:

  1. Load tlab_top pointer (there is typically a dedicated CPU register for thread locals on x64).
  2. Increment it by the size of an object.
  3. Compare tlab_top + object_size against tlab_end. Jump to the slow path if needed.
  4. Store the new value of tlab_top. The previous value is the address of newly created object.
  5. Set the default header of the object.
  6. Set the klass field - the pointer to object's class metadata.
  7. Initialize the rest object data with zeros. Even an object without fields may have an alignment padding.

These all is about 10-15 CPU instructions.

Let's measure the average time of an object creation with JMH benchmark.

package bench;

import org.openjdk.jmh.annotations.Benchmark;

public class Alloc {

    @Benchmark
    public Object baseline() {
        return "Some preallocated object";
    }

    @Benchmark
    public Object newObject() {
        return new Object();
    }
}

Results:

Benchmark        Mode  Cnt  Score   Error  Units
Alloc.baseline   avgt   10  3,428 ± 0,089  ns/op
Alloc.newObject  avgt   10  4,505 ± 0,056  ns/op

So, the object allocation along with benchmarking overhead takes ~4.5 ns or around 11 cycles on a 2.4GHz CPU. That's indeed cheap comparing to UUID generation algorithms.

这篇关于Java中对象创建需要多少个cpu周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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