 | IJVMWrapperDirectNewDirectBuffer(MemoryStream, Boolean, EventHandlerMemoryStream, Int32) Method |
Namespace: MASES.JCOBridge.C2JBridge.JVMInteropAssembly: C2JBridge (in C2JBridge.dll) Version: 2.6.9.260611-9a148513a79c26cdd7f1dde468f4f9e06ef3bc7e
SyntaxJCOBridgeDirectBuffer<byte> NewDirectBuffer(
MemoryStream stream,
bool useMemoryControlBlock = false,
EventHandler<MemoryStream> disposeEvent = null,
int timeToLive = -1
)
Function NewDirectBuffer (
stream As MemoryStream,
Optional useMemoryControlBlock As Boolean = false,
Optional disposeEvent As EventHandler(Of MemoryStream) = Nothing,
Optional timeToLive As Integer = -1
) As JCOBridgeDirectBuffer(Of Byte)
Parameters
- stream MemoryStream
- The non-disposed MemoryStream whose content will be copied into an internal buffer shared with the JVM via a https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html.
- useMemoryControlBlock Boolean (Optional)
- NO MORE USED STARTING FROM VERSION 2.6.9 - Appends to the end of the stream a memory block used to control and arbitrate memory between CLR and JVM.
- disposeEvent EventHandlerMemoryStream (Optional)
- An optional EventHandlerTEventArgs used to be informed when the internal copy derived from stream is no longer needed by the JVM,
allowing the caller to dispose stream at the right time. If , the underlying system will automatically dispose the MemoryStream.
- timeToLive Int32 (Optional)
- The time to live, expressed in milliseconds, the internal copy shall remain available; if the time to live expires the memory is retired leaving potentially the JVM under the possibility of an access violation.
Return Value
JCOBridgeDirectBufferByteA new instance of
JCOBridgeDirectBufferT holding an internal copy of
stream shared with the
https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html.
Exceptions
Remarks
Starting from version 2.6.9, the content of
stream is no longer pinned in memory but copied into an internal buffer managed by the subsystem.
Prior to version 2.6.9, the memory had to be pinned for the entire duration of the JVM usage to prevent access violations; the introduction of the internal copy removes this constraint,
allowing
stream to have an arbitrary lifetime without impacting the correctness of the operation.
The internal copy remains available until the JVM reference of the newly created
https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html is garbage collected.
Under heavy pressure the memory footprint can raise up and generate an
OutOfMemoryException, use the functionality with caution or take into account the
timeToLive option which can help to recover the memory in advance before the Garbage Collector of the JVM retires the
https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html.
If the user of
JCOBridgeDirectBufferT is pretty sure that the internal copy is no longer needed from the JVM, e.g. the invoked method does not queue the
https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html and its lifetime ends when the method returns, invoke
Dispose to immediately release unmanaged resources and free the memory.
High-rate scenarios: for activities with a high invocation rate, consider using RentT(Int64) to obtain a pooled JCOBridgeSharedBufferStreamT,
fill it using Stream-based APIs, and then pass the populated instance to NewDirectBufferT(JCOBridgeSharedBufferStreamT).
That overload automatically manages the lifecycle of the JCOBridgeSharedBufferStreamT by returning it to the pool once the JVM Garbage Collector retires the associated
https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html, avoiding repeated allocation and copy overhead.
See Also