From 06a9b2e22a3ad2f42d099bca92a5267c15586944 Mon Sep 17 00:00:00 2001 From: Wolfgang Hoschek Date: Wed, 29 Nov 2006 02:29:19 +0000 Subject: [PATCH] better estimation of memory consumption git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@480339 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/index/memory/MemoryIndex.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java index 995a77a75d4..6c4425fbf2b 100644 --- a/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java +++ b/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java @@ -1112,12 +1112,14 @@ public class MemoryIndex { public static final int FLOAT = 4; public static final int DOUBLE = 8; + private static final int LOG_PTR = (int) Math.round(log2(PTR)); + /** * Object header of any heap allocated Java object. * ptr to class, info for monitor, gc, hash, etc. */ - private static final int OBJECT_HEADER = 2*4; // typically even on 64 bit VMs -// private static final int OBJECT_HEADER = 2*PTR; +// private static final int OBJECT_HEADER = 2*4; // even on 64 bit VMs? + private static final int OBJECT_HEADER = 2*PTR; /** * Modern VMs tend to trade space for time, allocating memory on word @@ -1139,7 +1141,8 @@ public class MemoryIndex { // 9..16 --> 2*PTR private static int sizeOf(int n) { return IS_WORD_ALIGNED_VM ? - ((n-1)/PTR + 1) * PTR : +// ((n-1)/PTR + 1) * PTR : // slow version + (((n-1) >> LOG_PTR) + 1) << LOG_PTR : // fast version n; } @@ -1188,6 +1191,11 @@ public class MemoryIndex { return false; // better safe than sorry (applets, security managers, etc.) ... } } + + /** logarithm to the base 2. Example: log2(4) == 2, log2(8) == 3 */ + private static double log2(double value) { + return Math.log(value) / Math.log(2); + } }