- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
本文整理了Java中com.yahoo.memory.WritableMemory.allocateDirect()
方法的一些代码示例,展示了WritableMemory.allocateDirect()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WritableMemory.allocateDirect()
方法的具体详情如下:
包路径:com.yahoo.memory.WritableMemory
类名称:WritableMemory
方法名:allocateDirect
[英]Allocates and provides access to capacityBytes directly in native (off-heap) memory leveraging the WritableMemory API. Native byte order is assumed. The allocated memory will be 8-byte aligned, but may not be page aligned. If capacityBytes is zero, byte order, backing storage and read-only status of the WritableMemory object, returned from WritableHandle#get() are unspecified.
The default MemoryRequestServer, which allocates any request for memory onto the heap, will be used.
NOTE: Native/Direct memory acquired using Unsafe may have garbage in it. It is the responsibility of the using class to clear this memory, if required, and to call close() when done.
[中]利用WritableMemory API直接在本机(堆外)内存中分配和提供对容量字节的访问。假定为本机字节顺序。分配的内存将按8字节对齐,但可能不按页面对齐。如果capacityBytes为零,则未指定从WritableHandle#get()返回的WritableMemory对象的字节顺序、后备存储和只读状态。
将使用默认的MemoryRequestServer,它将任何内存请求分配到堆中。
注意:使用“不安全”获取的本机/直接内存中可能有垃圾。如果需要,使用类负责清除该内存,并在完成时调用close()。
代码示例来源:origin: DataSketches/sketches-core
private static WritableDirectHandle makeNativeMemory(int k) {
return WritableMemory.allocateDirect(getMaxBytes(k));
}
代码示例来源:origin: DataSketches/sketches-core
private static WritableDirectHandle makeNativeMemory(int k) {
return WritableMemory.allocateDirect(getMaxBytes(k));
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkEmptyDirect() {
try (WritableDirectHandle wdh = WritableMemory.allocateDirect(1000)) {
WritableMemory mem = wdh.get();
UpdateDoublesSketch sketch = DoublesSketch.builder().build(mem);
sketch.toByteArray(); //exercises a specific path
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void directSketchShouldMoveOntoHeapEventually() {
try (WritableDirectHandle wdh = WritableMemory.allocateDirect(1000)) {
WritableMemory mem = wdh.get();
UpdateDoublesSketch sketch = DoublesSketch.builder().build(mem);
Assert.assertTrue(sketch.isSameResource(mem));
for (int i = 0; i < 1000; i++) {
sketch.update(i);
}
Assert.assertFalse(sketch.isSameResource(mem));
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkMoveAndResize() {
int k = 1 << 12;
int u = 2 * k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
try (WritableDirectHandle wdh = WritableMemory.allocateDirect(bytes/2)) { //will request
WritableMemory wmem = wdh.get();
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
assertTrue(sketch.isSameResource(wmem));
for (int i = 0; i < u; i++) { sketch.update(i); }
assertFalse(sketch.isSameResource(wmem));
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkMoveAndResize() {
int k = 1 << 12;
int u = 2 * k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
try (WritableDirectHandle wdh = WritableMemory.allocateDirect(bytes/2);
WritableDirectHandle wdh2 = WritableMemory.allocateDirect(bytes/2) ) {
WritableMemory wmem = wdh.get();
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
assertTrue(sketch.isSameResource(wmem));
WritableMemory wmem2 = wdh2.get();
Union union = SetOperation.builder().buildUnion(wmem2);
assertTrue(union.isSameResource(wmem2));
for (int i = 0; i < u; i++) { union.update(i); }
assertFalse(union.isSameResource(wmem));
Union union2 = SetOperation.builder().buildUnion(); //on-heap union
assertFalse(union2.isSameResource(wmem2)); //obviously not
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkEstModeNativeMemory() {
int k = 4096;
int u = 2*k;
int memCapacity = (k << 4) + (Family.QUICKSELECT.getMinPreLongs() << 3);
try(WritableDirectHandle memHandler = WritableMemory.allocateDirect(memCapacity)) {
UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(memHandler.get());
DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch)usk; //for internal checks
assertTrue(usk.isEmpty());
for (int i = 0; i< u; i++) { usk.update(i); }
double est = usk.getEstimate();
println(""+est);
assertEquals(usk.getEstimate(), u, u*.05);
assertTrue(sk1.getRetainedEntries(false) > k);
}
}
代码示例来源:origin: DataSketches/memory
/**
* Allocates and provides access to capacityBytes directly in native (off-heap) memory
* leveraging the WritableMemory API. Native byte order is assumed.
* The allocated memory will be 8-byte aligned, but may not be page aligned.
* If capacityBytes is zero, byte order, backing storage and read-only status
* of the WritableMemory object, returned from {@link WritableHandle#get()} are unspecified.
*
* <p>The default MemoryRequestServer, which allocates any request for memory onto the heap,
* will be used.</p>
*
* <p><b>NOTE:</b> Native/Direct memory acquired using Unsafe may have garbage in it.
* It is the responsibility of the using class to clear this memory, if required,
* and to call <i>close()</i> when done.</p>
*
* @param capacityBytes the size of the desired memory in bytes.
* @return WritableDirectHandle for this off-heap resource.
* Please read Javadocs for {@link Handle}.
*/
public static WritableDirectHandle allocateDirect(final long capacityBytes) {
return allocateDirect(capacityBytes, null);
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkEstModeNativeMemory() {
lgK = 12;
int k = 1 << lgK;
int u = 2*k;
int memCapacity = (k << 4) + (Family.QUICKSELECT.getMinPreLongs() << 3);
try(WritableDirectHandle memHandler = WritableMemory.allocateDirect(memCapacity)) {
final ConcurrentThetaBuilder bldr = configureBuilder();
//must build shared first
shared = bldr.build(memHandler.get());
UpdateSketch usk = bldr.build();
assertTrue(usk.isEmpty());
for (int i = 0; i< u; i++) { usk.update(i); }
waitForPropagationToComplete();
double est = usk.getEstimate();
println(""+est);
assertEquals(usk.getEstimate(), u, u*.05);
assertTrue(shared.getSharedRetainedEntries(false) > k);
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkGrowBaseBuf() {
final int k = 128;
final int u = 32; // don't need the BB to fill here
final int initBytes = (4 + (u / 2)) << 3; // not enough to hold everything
try (WritableDirectHandle memHandler = WritableMemory.allocateDirect(initBytes)) {
//final MemoryManager memMgr = new MemoryManager();
//final WritableMemory mem1 = memMgr.request(initBytes);
final WritableMemory mem1 = memHandler.get();
println("Initial mem size: " + mem1.getCapacity());
final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(mem1);
for (int i = 1; i <= u; i++) {
usk1.update(i);
}
final int currentSpace = usk1.getCombinedBufferItemCapacity();
println("curCombBufItemCap: " + currentSpace);
assertEquals(currentSpace, 2 * k);
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkGrowFromWrappedEmptySketch() {
final int k = 16;
final int n = 0;
final int initBytes = DoublesSketch.getUpdatableStorageBytes(k, n); //8 bytes
final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build();
final Memory origSketchMem = Memory.wrap(usk1.toByteArray());
try (WritableDirectHandle memHandle = WritableMemory.allocateDirect(initBytes)) {
WritableMemory mem = memHandle.get();
origSketchMem.copyTo(0, mem, 0, initBytes);
UpdateDoublesSketch usk2 = DirectUpdateDoublesSketch.wrapInstance(mem);
assertTrue(mem.isSameResource(usk2.getMemory()));
assertEquals(mem.getCapacity(), initBytes);
assertTrue(mem.isDirect());
assertTrue(usk2.isEmpty());
//update the sketch forcing it to grow on-heap
for (int i = 1; i <= 5; i++) { usk2.update(i); }
assertEquals(usk2.getN(), 5);
WritableMemory mem2 = usk2.getMemory();
assertFalse(mem.isSameResource(mem2));
assertFalse(mem2.isDirect()); //should now be on-heap
final int expectedSize = COMBINED_BUFFER + ((2 * k) << 3);
assertEquals(mem2.getCapacity(), expectedSize);
}
}
代码示例来源:origin: DataSketches/sketches-core
try (WritableDirectHandle hand = WritableMemory.allocateDirect(bytes)) {
wmem = hand.get();
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkReadOnlyRebuildResize() {
int k = 1 << 12;
int u = 2 * k;
int bytes = Sketches.getMaxUpdateSketchBytes(k);
try (WritableDirectHandle wdh = WritableMemory.allocateDirect(bytes/2)) { //will request
WritableMemory wmem = wdh.get();
UpdateSketch sketch = Sketches.updateSketchBuilder().setNominalEntries(k).build(wmem);
for (int i = 0; i < u; i++) { sketch.update(i); }
double est1 = sketch.getEstimate();
byte[] ser = sketch.toByteArray();
Memory mem = Memory.wrap(ser);
UpdateSketch roSketch = (UpdateSketch) Sketches.wrapSketch(mem);
double est2 = roSketch.getEstimate();
assertEquals(est2, est1);
try {
roSketch.rebuild();
fail();
} catch (SketchesReadOnlyException e) {
//expected
}
try {
roSketch.reset();
fail();
} catch (SketchesReadOnlyException e) {
//expected
}
}
}
代码示例来源:origin: DataSketches/sketches-core
Memory heapROMem;
try (WritableDirectHandle wdh = WritableMemory.allocateDirect(bytes)) {
directMem = wdh.get();
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkUnionCompactOrderedSource() {
int k = 1 << 12;
UpdateSketch sk = Sketches.updateSketchBuilder().build();
for (int i = 0; i < k; i++) { sk.update(i); }
double est1 = sk.getEstimate();
int bytes = Sketches.getMaxCompactSketchBytes(sk.getRetainedEntries());
try (WritableDirectHandle h = WritableMemory.allocateDirect(bytes)) {
WritableMemory wmem = h.get();
CompactSketch csk = sk.compact(true, wmem); //ordered, direct
Union union = Sketches.setOperationBuilder().buildUnion();
union.update(csk);
double est2 = union.getResult().getEstimate();
assertEquals(est2, est1);
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkLimitedMemoryScenarios() { //Requesting application
final int k = 128;
final int u = 40 * k;
final int initBytes = ((2 * k) + 4) << 3; //just the BB
//########## Owning Implementation
// This part would actually be part of the Memory owning implemention so it is faked here
try (WritableDirectHandle wdh = WritableMemory.allocateDirect(initBytes)) {
final WritableMemory wmem = wdh.get();
println("Initial mem size: " + wmem.getCapacity());
//########## Receiving Application
// The receiving application has been given wmem to use for a sketch,
// but alas, it is not ultimately large enough.
final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(wmem);
assertTrue(usk1.isEmpty());
//Load the sketch
for (int i = 0; i < u; i++) {
// The sketch uses The MemoryRequest, acquired from wmem, to acquire more memory as
// needed, and requests via the MemoryRequest to free the old allocations.
usk1.update(i);
}
final double result = usk1.getQuantile(0.5);
println("Result: " + result);
assertEquals(result, u / 2.0, 0.05 * u); //Success
//########## Owning Implementation
//The actual Memory has been re-allocated several times,
// so the above wmem reference is invalid.
println("\nFinal mem size: " + wmem.getCapacity());
}
}
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkGrowCombBuf() {
final int k = 128;
final int u = (2 * k) - 1; //just to fill the BB
final int initBytes = ((2 * k) + 4) << 3; //just room for BB
try (WritableDirectHandle memHandler = WritableMemory.allocateDirect(initBytes)) {
//final MemoryManager memMgr = new MemoryManager();
//final WritableMemory mem1 = memMgr.request(initBytes);
final WritableMemory mem1 = memHandler.get();
println("Initial mem size: " + mem1.getCapacity());
final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(mem1);
for (int i = 1; i <= u; i++) {
usk1.update(i);
}
final int currentSpace = usk1.getCombinedBufferItemCapacity();
println("curCombBufItemCap: " + currentSpace);
final double[] newCB = usk1.growCombinedBuffer(currentSpace, 3 * k);
final int newSpace = usk1.getCombinedBufferItemCapacity();
println("newCombBurItemCap: " + newSpace);
assertEquals(newCB.length, 3 * k);
//memMgr.free(mem1);
}
}
代码示例来源:origin: DataSketches/sketches-core
int bytes = HllSketch.getMaxUpdatableSerializationBytes(lgConfigK, tgtHllType);
HllSketch hllSketch;
try (WritableDirectHandle handle = WritableMemory.allocateDirect(bytes)) {
WritableMemory wmem = handle.get();
hllSketch = new HllSketch(lgConfigK, tgtHllType, wmem);
代码示例来源:origin: DataSketches/sketches-core
@Test
public void checkInsertsAndExtracts() {
final int bytes = 32;
try (WritableDirectHandle offHeapMemHandler = WritableMemory.allocateDirect(bytes)) {
final WritableMemory offHeapMem = offHeapMemHandler.get();
final WritableMemory onHeapMem = WritableMemory.wrap(new byte[bytes]);
我想知道当没有可用内存时,allocateDirect 和 allocate from ByteBuffer 会返回空指针还是其他什么? (我在java文档中找不到它)。提前致谢。 最佳答案 对于这两
我使用浮点缓冲区作为 Android 中 opengl 绘图所需的直接字节缓冲区。问题是,在创建字节缓冲区时,GC 会变得疯狂——就像 30 多秒一样疯狂。我正在创建一个 40x40 顶点、1600
我今天读了以下内容: Direct ByteBuffer objects clean up their native buffers automatically but can only do so
根据 various sources (尽管在 JavaDoc 中没有特别提到),ByteBuffer.allocateDirect 分配主 JVM 堆之外的内存。我可以确认使用 Java Missi
本文整理了Java中com.yahoo.memory.WritableMemory.allocateDirect()方法的一些代码示例,展示了WritableMemory.allocateDirect
我正在研究一些SocketChannel到SocketChannel的代码,这些代码最适合直接字节缓冲区-长寿且很大(每个连接数十到数百兆字节)。在用FileChannel散列确切的循环结构时,我运行
我刚刚解决了一个错误,但我不知道为什么。在 LWJGL 中创建 4x4 投影矩阵,用于顶点着色器.. 该行会导致问题。它会默默地失败,并且着色器中的 mat4 被卡为全零(就好像它从未被写入一样)。
分配零长度缓冲区通常取决于实现。 我知道 malloc(0) 返回 NULL 或可以安全释放的指针,这完全取决于实现。 那么,当调用 ByteBuffer.allocateDirect(0) 时会发生
我正在尝试制作堆外内存缓冲区。我想要非常大的缓冲区(比如 10GB)。我听说 jvm 堆有时会因为 full GC 而卡住。因此,我尝试使用 java.nio.ByteBuffer 制作缓冲区。 但是
我试图通过使用 MappedByteBuffer 对特定文件进行内存映射,在两个或多个 JVM 之间实现一种共享缓存。从规范中我看到,当我们使用 MappedByteBuffer.load() 时,它
我在以下代码中得到了 UnsupportedOperationException: byte[] temp = ByteBuffer.allocateDirect(10).array(); 我检查了j
为什么 CharBuffer 中有 isDirect() 方法? 如果 CharBuffer 或 Buffer 中没有任何相应的 (allocateDirect) 方法,我们如何分配直接 CharBu
我是安卓新手。我似乎找不到相关的论坛帖子。 令我困惑的是 allocateDirect() 确实在 Android 4.2 模拟器中创建支持 byte[]。 更具体地说,我分配一个 ByteBuffe
我有一个每 600 毫秒运行一次的游戏服务器,在一个周期内操作一个字节数组,然后在周期结束时将字节数组写入客户端。 由于在循环结束时需要写入多少字节的不确定性,我在循环结束时为每次写入创建了一个字节缓
从示例中复制并粘贴源代码是一回事,但我正在寻找一些答案来解释为什么示例是这样的。 我无法回答的一个问题是为什么三角形的 ByteBuffer 每个坐标需要四个字节。 在 example present
我是一名优秀的程序员,十分优秀!