public void release ( MemorySegment segment ) { // check if segment is null or has already been freed if ( segment == null || segment . getOwner ( ) == null ) { return ; } final Object owner = segment . getOwner ( ) ; // -------------------- BEGIN CRITICAL SECTION ------------------- synchronized ( lock ) { // prevent double return to this memory manager if ( segment . isFreed ( ) ) { return ; } if ( isShutDown ) { throw new IllegalStateException ( "Memory manager has been shut down." ) ; } // remove the reference in the map for the owner try { Set < MemorySegment > segsForOwner = this . allocatedSegments . get ( owner ) ; if ( segsForOwner != null ) { segsForOwner . remove ( segment ) ; if ( segsForOwner . isEmpty ( ) ) { this . allocatedSegments . remove ( owner ) ; } } if ( isPreAllocated ) { // release the memory in any case memoryPool . returnSegmentToPool ( segment ) ; } else { segment . free ( ) ; numNonAllocatedPages ++ ; } } catch ( Throwable t ) { throw new RuntimeException ( "Error removing book-keeping reference to allocated memory segment." , t ) ; } } // -------------------- END CRITICAL SECTION ------------------- }
Tries to release the memory for the specified segment . If the segment has already been released or is null the request is simply ignored .