Skip to content

Commit

Permalink
Add finalizer as "cleanup method of last resort"
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosen committed Apr 22, 2015
1 parent c754ae1 commit ae39694
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ public BytesToBytesMap(MemoryAllocator allocator, long initialCapacity) {
this(allocator, initialCapacity, 0.70);
}

// TODO: consider finalizer.
@Override
public void finalize() {
// In case the programmer forgot to call `free()`, try to perform that cleanup now:
free();
}

/**
* Returns the number of keys defined in the map.
Expand Down Expand Up @@ -457,12 +461,18 @@ private void allocate(long capacity) {
/**
* Free all allocated memory associated with this map, including the storage for keys and values
* as well as the hash map array itself.
*
* This method is idempotent.
*/
public void free() {
allocator.free(longArray.memoryBlock());
longArray = null;
allocator.free(bitset.memoryBlock());
bitset = null;
if (longArray != null) {
allocator.free(longArray.memoryBlock());
longArray = null;
}
if (bitset != null) {
allocator.free(bitset.memoryBlock());
bitset = null;
}
Iterator<MemoryBlock> dataPagesIterator = dataPages.iterator();
while (dataPagesIterator.hasNext()) {
allocator.free(dataPagesIterator.next());
Expand Down

0 comments on commit ae39694

Please sign in to comment.