# Class MemorySegmentRequestExample

java.lang.Object
org.apache.datasketches.common.MemorySegmentRequestExample

All Implemented Interfaces:
  `MemorySegmentRequest`

public final class MemorySegmentRequestExample extends Object implements MemorySegmentRequest
This is an example of a possible implementation of the MemorySegmentRequest interface where all requested segments are allocated off-heap. A local ConcurrentHashMap tracks a newly created confined Arena for every new MemorySegment allocated off-heap. This allows individual segments to be freed immediately upon receiving the `requestClose(MemorySegment)` call.

## Nested Class Summary

### Nested classes/interfaces inherited from interface MemorySegmentRequest

`MemorySegmentRequest.Default`

## Field Summary

### Fields inherited from interface MemorySegmentRequest

`DEFAULT`

## Constructor Summary

Constructors
Constructor
Description
`MemorySegmentRequestExample()`
No argument constructor.

## Method Summary

Modifier and Type
Method
Description
`void`
`cleanup()`
This closes any unclosed, off-heap MemorySegments and removes all mappings from the map.
`MemorySegment`
`request(long newByteSize)`
Request a new off-heap MemorySegment with the given newByteSeze.
`void`
`requestClose(MemorySegment segKey)`
Request to close the given MemorySegment.

### Methods inherited from class Object

`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### MemorySegmentRequestExample

public MemorySegmentRequestExample()
No argument constructor.

## Method Details

### request

public MemorySegment request(long newByteSize)
Request a new off-heap MemorySegment with the given newByteSeze. An internal confined Arena is created to exclusively manage the new segment and it is associated with the new segment with a ConcurrentHashMap.

Specified by:
  `request` in interface `MemorySegmentRequest`
Parameters:
  `newByteSize` - The new byteSize being requested.
Returns:
  new MemorySegment with the requested byteSize.

### requestClose

public void requestClose(MemorySegment segKey)
Description copied from interface: `MemorySegmentRequest`
Request to close the given MemorySegment. Because we do not have a reference to an Arena, the default here is to do nothing. It is up to the user to override this as appropriate.

Specified by:
  `requestClose` in interface `MemorySegmentRequest`
Parameters:
  `segKey` - the previous MemorySegment to be closed.

### cleanup

public void cleanup()
This closes any unclosed, off-heap MemorySegments and removes all mappings from the map.