chronicle值:如果它仍然需要在堆上分配示例,有什么意义?

zf2sa74q  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(328)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

12天前关门了。
改进这个问题
我很难理解如何正确使用这个库,因为它在每个分配中创建垃圾,这似乎就是我的意图?库是否打算与这些特性一起使用?或者我遗漏了什么,还有别的分配方法吗?
例如:

package org.jire;

import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.NativeBytesStore;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.values.Values;

public class Test {
    public static void main(String[] args) {
        NativeBytesStore<Void> store = NativeBytesStore.nativeStoreWithFixedCapacity(4);
        while (true) {
            IntValue value = Values.newNativeReference(IntValue.class);
            ((Byteable) value).bytesStore(store, 0, 4);
        }
    }
}

在yourkit中分析之后,您可以看到有许多分配(垃圾)正在生成:

mu0hgdu0

mu0hgdu01#

您需要在堆上至少创建一个示例。
但是,一旦您完成了这项工作,它就可以用作指向本机内存中任何区域的flyweight。
在本例中,数据是堆大小的许多倍,但不使用堆。

import net.openhft.chronicle.bytes.Byteable;
import net.openhft.chronicle.bytes.NativeBytesStore;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.values.Values;

public class Test {
    public static void main(String[] args) {
        System.out.println("Run with -Xmx64m -verbose:gc -ea");
        int size = 250_000_000;
        IntValue value = Values.newNativeReference(IntValue.class);
        Byteable valuePtr = (Byteable) value;
        // NOTE: This is much, much larger than the heap size
        NativeBytesStore<Void> store = NativeBytesStore.nativeStoreWithFixedCapacity(size * Integer.BYTES);

        for (long t = 1; ; t++) {
            // set all the values
            for (int i = 0; i < size; i++) {
                valuePtr.bytesStore(store, i * Integer.BYTES, Integer.BYTES);
                boolean set = value.compareAndSwapValue(0, i);
                assert set;
            }
            System.out.print("set " + t * size + ", ");

            // check and unset all the values
            for (int i = 0; i < size; i++) {
                valuePtr.bytesStore(store, i * Integer.BYTES, Integer.BYTES);
                boolean set = value.compareAndSwapValue(i, 0);
                assert set;
            }
            System.out.println("unset " + t * size);
        }
    }
}

与-xmx64m一起运行-verbose:gc -ea 启动时打印一些gc,但运行时不生成任何对象。

Run with -Xmx64m -verbose:gc -ea
[main] INFO net.openhft.chronicle.core.Jvm - Chronicle core loaded from file:/C:/cygwin64/home/peter/Build-All/Chronicle-Core/target/classes/
[GC (Allocation Failure)  16384K->2072K(62976K), 0.0016387 secs]
[GC (Allocation Failure)  18456K->2376K(62976K), 0.0016103 secs]
[GC (Allocation Failure)  18760K->5979K(62976K), 0.0039673 secs]
[GC (Allocation Failure)  22363K->9703K(62976K), 0.0152631 secs]
set 250000000, unset 250000000
set 500000000, unset 500000000
set 750000000, unset 750000000
set 1000000000, unset 1000000000
set 1250000000, unset 1250000000
set 1500000000, unset 1500000000
set 1750000000, unset 1750000000
set 2000000000, unset 2000000000
set 2250000000, unset 2250000000
set 2500000000, unset 2500000000
set 2750000000, unset 2750000000
set 3000000000, unset 3000000000
set 3250000000, unset 3250000000
set 3500000000, unset 3500000000
set 3750000000, unset 3750000000
set 4000000000, unset 4000000000
set 4250000000, unset 4250000000
set 4500000000, unset 4500000000
set 4750000000, unset 4750000000
set 5000000000, unset 5000000000

现在,为了让您真正感兴趣,您可以将它Map到一个文件,这个文件可以由多个进程同时访问。

import net.openhft.chronicle.bytes.*;
import net.openhft.chronicle.core.values.IntValue;
import net.openhft.chronicle.values.Values;

import java.io.File;
import java.io.FileNotFoundException;

public class Test {
    public static void main(String[] args) throws FileNotFoundException {
        System.out.println("Run with -Xmx64m -verbose:gc -ea");
        int size = 250_000_000;
        IntValue value = Values.newNativeReference(IntValue.class);
        Byteable valuePtr = (Byteable) value;
        // NOTE: This is much, much larger than the heap size
        File deleteme = new File("deleteme");
        deleteme.delete(); // has to delete or there will be some data later.
        BytesStore store = MappedBytes.mappedBytes(deleteme, size * Integer.BYTES);

        for (long t = 1; ; t++) {
            long start = System.nanoTime();
            // set all the values
            for (int i = 0; i < size; i++) {
                valuePtr.bytesStore(store, i * Integer.BYTES, Integer.BYTES);
                boolean set = value.compareAndSwapValue(0, i);
                assert set;
            }
            System.out.print("set " + t * size + ", ");

            // check and unset all the values
            for (int i = 0; i < size; i++) {
                valuePtr.bytesStore(store, i * Integer.BYTES, Integer.BYTES);
                boolean set = value.compareAndSwapValue(i, 0);
                assert set;
            }
            System.out.print("unset " + t * size);
            long time = (System.nanoTime() - start) / size;
            System.out.println(", avg time " + time+ " ns to set/unset.");
        }
    }
}

印刷品

Run with -Xmx64m -verbose:gc -ea
[main] INFO net.openhft.chronicle.core.Jvm - Chronicle core loaded from file:/C:/cygwin64/home/peter/Build-All/Chronicle-Core/target/classes/
[GC (Allocation Failure)  16384K->2072K(62976K), 0.0014760 secs]
[GC (Allocation Failure)  18456K->2392K(62976K), 0.0021388 secs]
[GC (Allocation Failure)  18722K->5839K(62976K), 0.0043109 secs]
[GC (Allocation Failure)  22223K->9738K(62976K), 0.0052064 secs]
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 8506 us to grow file deleteme
[main] INFO net.openhft.chronicle.bytes.MappedFile - Took 17 ms to add mapping for deleteme
set 250000000, unset 250000000, avg time 14 ns to set/unset.
set 500000000, unset 500000000, avg time 12 ns to set/unset.
set 750000000, unset 750000000, avg time 13 ns to set/unset.
set 1000000000, unset 1000000000, avg time 13 ns to set/unset.
set 1250000000, unset 1250000000, avg time 13 ns to set/unset.
set 1500000000, unset 1500000000, avg time 12 ns to set/unset.
set 1750000000, unset 1750000000, avg time 12 ns to set/unset.
set 2000000000, unset 2000000000, avg time 13 ns to set/unset.
set 2250000000, unset 2250000000, avg time 12 ns to set/unset.
set 2500000000, unset 2500000000, avg time 12 ns to set/unset.
set 2750000000, unset 2750000000, avg time 12 ns to set/unset.
set 3000000000, unset 3000000000, avg time 13 ns to set/unset.
set 3250000000, unset 3250000000, avg time 13 ns to set/unset.
set 3500000000, unset 3500000000, avg time 13 ns to set/unset.
set 3750000000, unset 3750000000, avg time 13 ns to set/unset.
set 4000000000, unset 4000000000, avg time 13 ns to set/unset.
set 4250000000, unset 4250000000, avg time 13 ns to set/unset.
set 4500000000, unset 4500000000, avg time 13 ns to set/unset.
set 4750000000, unset 4750000000, avg time 12 ns to set/unset.
set 5000000000, unset 5000000000, avg time 12 ns to set/unset.

注意:将数据写入文件并稍后再读取平均只需13纳秒。

相关问题