LUCENE-5053: Expose PagedGrowableWriter memory usage.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1492114 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrien Grand 2013-06-12 08:34:27 +00:00
parent a62bdaa944
commit a4d58b6f22
3 changed files with 28 additions and 1 deletions

View File

@ -20,6 +20,7 @@ package org.apache.lucene.util.packed;
import java.io.IOException;
import org.apache.lucene.store.DataOutput;
import org.apache.lucene.util.RamUsageEstimator;
/**
* Implements {@link PackedInts.Mutable}, but grows the
@ -123,7 +124,12 @@ public class GrowableWriter implements PackedInts.Mutable {
@Override
public long ramBytesUsed() {
return current.ramBytesUsed();
return RamUsageEstimator.alignObjectSize(
RamUsageEstimator.NUM_BYTES_OBJECT_HEADER
+ RamUsageEstimator.NUM_BYTES_OBJECT_REF
+ RamUsageEstimator.NUM_BYTES_LONG
+ RamUsageEstimator.NUM_BYTES_FLOAT)
+ current.ramBytesUsed();
}
@Override

View File

@ -20,6 +20,8 @@ package org.apache.lucene.util.packed;
import static org.apache.lucene.util.packed.PackedInts.checkBlockSize;
import static org.apache.lucene.util.packed.PackedInts.numBlocks;
import org.apache.lucene.util.RamUsageEstimator;
/**
* A {@link PagedGrowableWriter}. This class slices data into fixed-size blocks
* which have independent numbers of bits per value and grow on-demand.
@ -128,6 +130,21 @@ public final class PagedGrowableWriter {
return newWriter;
}
/** Return the number of bytes used by this object. */
public long ramBytesUsed() {
long bytesUsed = RamUsageEstimator.alignObjectSize(
RamUsageEstimator.NUM_BYTES_OBJECT_HEADER
+ RamUsageEstimator.NUM_BYTES_OBJECT_REF
+ RamUsageEstimator.NUM_BYTES_LONG
+ 3 * RamUsageEstimator.NUM_BYTES_INT
+ RamUsageEstimator.NUM_BYTES_FLOAT);
bytesUsed += RamUsageEstimator.alignObjectSize(RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + (long) RamUsageEstimator.NUM_BYTES_OBJECT_REF * subWriters.length);
for (GrowableWriter gw : subWriters) {
bytesUsed += gw.ramBytesUsed();
}
return bytesUsed;
}
@Override
public String toString() {
return getClass().getSimpleName() + "(size=" + size() + ",pageSize=" + pageSize() + ")";

View File

@ -657,6 +657,7 @@ public class TestPackedInts extends LuceneTestCase {
assertEquals(10, wrt.get(7));
assertEquals(99, wrt.get(valueCount - 10));
assertEquals(1 << 10, wrt.get(valueCount - 1));
assertEquals(RamUsageEstimator.sizeOf(wrt), wrt.ramBytesUsed());
}
public void testPagedGrowableWriter() {
@ -684,6 +685,9 @@ public class TestPackedInts extends LuceneTestCase {
assertEquals(buf.get(i), writer.get(i));
}
// test ramBytesUsed
assertEquals(RamUsageEstimator.sizeOf(writer), writer.ramBytesUsed());
// test copy
PagedGrowableWriter copy = writer.resize(_TestUtil.nextLong(random(), writer.size() / 2, writer.size() * 3 / 2));
for (long i = 0; i < copy.size(); ++i) {