@@ -70,6 +70,7 @@ public class TagManager {
7070 private static final String PREVIOUS_CONDITION =
7171 "before deleting it, tag key is %s, tag value is %s, tlog offset is %d, contains key %b" ;
7272
73+ private static final long COLLECTION_SIZE_FIELD_BYTES = Integer .BYTES ;
7374 private static final Logger logger = LoggerFactory .getLogger (TagManager .class );
7475 private static final CommonConfig COMMON_CONFIG = CommonDescriptor .getInstance ().getConfig ();
7576
@@ -171,21 +172,18 @@ public void addIndex(String tagKey, String tagValue, IMeasurementMNode<?> measur
171172 long memorySize = 0 ;
172173 if (tagValueMap == null ) {
173174 tagValueMap = new ConcurrentHashMap <>();
174- // the last 4 is the memory occupied by the size of tagvaluemap
175- memorySize += RamUsageEstimator .sizeOf (tagKey ) + 4 ;
175+ memorySize += RamUsageEstimator .sizeOf (tagKey ) + COLLECTION_SIZE_FIELD_BYTES ;
176176 }
177177
178178 Set <IMeasurementMNode <?>> measurementsSet = tagValueMap .get (tagValue );
179179 if (measurementsSet == null ) {
180180 measurementsSet = ConcurrentHashMap .newKeySet ();
181181 tagValueMap .put (tagValue , measurementsSet );
182- // the last 4 is the memory occupied by the size of measurementsSet
183- memorySize += RamUsageEstimator .sizeOf (tagValue ) + 4 ;
182+ memorySize += RamUsageEstimator .sizeOf (tagValue ) + COLLECTION_SIZE_FIELD_BYTES ;
184183 }
185184
186185 if (measurementsSet .add (measurementMNode )) {
187- // 8 is the memory occupied by the length of the IMeasurementMNode
188- memorySize += RamUsageEstimator .NUM_BYTES_OBJECT_REF + 4 ;
186+ memorySize += RamUsageEstimator .NUM_BYTES_OBJECT_REF + COLLECTION_SIZE_FIELD_BYTES ;
189187 }
190188 if (memorySize > 0 ) {
191189 requestMemory (memorySize );
@@ -216,17 +214,15 @@ public void removeIndex(String tagKey, String tagValue, IMeasurementMNode<?> mea
216214 }
217215
218216 if (measurementsSet .remove (measurementMNode )) {
219- memorySize += RamUsageEstimator .NUM_BYTES_OBJECT_REF + 4 ;
217+ memorySize += RamUsageEstimator .NUM_BYTES_OBJECT_REF + COLLECTION_SIZE_FIELD_BYTES ;
220218 }
221219 if (measurementsSet .isEmpty ()) {
222220 if (tagValueMap .remove (tagValue , measurementsSet )) {
223- // the last 4 is the memory occupied by the size of IMeasurementMNodeSet
224- memorySize += RamUsageEstimator .sizeOf (tagValue ) + 4 ;
221+ memorySize += RamUsageEstimator .sizeOf (tagValue ) + COLLECTION_SIZE_FIELD_BYTES ;
225222 }
226223 }
227224 if (tagValueMap .isEmpty ()) {
228- // the last 4 is the memory occupied by the size of tagValueMap
229- memorySize += RamUsageEstimator .sizeOf (tagKey ) + 4 ;
225+ memorySize += RamUsageEstimator .sizeOf (tagKey ) + COLLECTION_SIZE_FIELD_BYTES ;
230226 if (memorySize > 0 ) {
231227 releaseMemory (memorySize );
232228 }
0 commit comments