< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/share/gc/Memory.java

Print this page

  1 /*
  2  * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 package nsk.share.gc;
 25 
 26 import nsk.share.test.LocalRandom;
 27 import java.io.PrintStream;
 28 import nsk.share.gc.gp.GarbageProducer;
 29 import nsk.share.gc.tree.*;
 30 import nsk.share.gc.gp.MemoryStrategy;
 31 import nsk.share.log.Log;
 32 
 33 /**
 34  *  Different utility methods to work with memory objects.
 35  */
 36 public final class Memory {
 37         private static int bits = 0;
 38         private static int referenceSize = 0;
 39         private static int objectExtraSize = 0;
 40 





 41         private Memory() {
 42         }
 43 
 44         private static int getBits() {
 45                 if (bits == 0)
 46                         bits = Integer.parseInt(System.getProperty("sun.arch.data.model"));
 47                 return bits;
 48         }
 49 
 50         /**
 51          *  Get size of one object reference.
 52          *
 53          *  TODO: somehow determine the real value
 54          */
 55         public static int getReferenceSize() {
 56                 if (referenceSize == 0)
 57                         referenceSize = (getBits() == 64) ? 8 : 4;
 58                 return referenceSize;
 59         }
 60 








 61         /**
 62          * Get size of primitive type int.
 63          */
 64         public static int getIntSize() {
 65                 return 4;
 66         }
 67 
 68         /**
 69          * Get size of primitive type boolean.
 70          */
 71         public static int getBooleanSize() {
 72                 return 1;
 73         }
 74 
 75         /**
 76          * Get size of primitive type byte.
 77          */
 78         public static int getByteSize() {
 79                 return 1;
 80         }

 98          */
 99         public static int getLongSize() {
100                 return 8;
101         }
102 
103         /**
104          * Get size of primitive type float.
105          */
106         public static int getFloatSize() {
107                 return 4;
108         }
109 
110         /**
111          * Get size of primitive type double.
112          */
113         public static int getDoubleSize() {
114                 return 8;
115         }
116 
117         /**








































118          *  Get how many extra bytes an object occupies in the heap
119          *  compared to sum of it's fields.
120          *
121          *  TODO: somehow determine the real value
122          */
123         public static int getObjectExtraSize() {
124                 if (objectExtraSize == 0)
125                         objectExtraSize = 2 * getReferenceSize();
126                 return objectExtraSize;
127         }
128         /**
129          *  Get how many extra bytes an array occupies in the heap
130          *  compared to sum of it's fields.
131          *
132          *  TODO: somehow determine the real value
133          */
134         public static int getArrayExtraSize() {
135                 return getObjectExtraSize();
136         }
137 
138         /**
139          * Return size of reference object (SoftReference, WeakReference, PhantomReference)
140          */
141         public static int getReferenceObjectSize() {
142                 return getReferenceSize() + getObjectExtraSize();
143         }
144 
145         /**
146          *  Get an approximate length of array that will occupy a given memory.
147          *
148          *  @param memory size of memory
149          *  @param objectSize size of each object in array
150          *  @return length of array
151          */
152         public static int getArrayLength(long memory, long objectSize) {
153                 int arrayExtraSize = getArrayExtraSize();
154                 return (int) Math.min((memory - arrayExtraSize) / objectSize,
155                         Integer.MAX_VALUE);

156         }
157 
158         /**
159          *  Get an approximate size of array of given length and object size.
160          *
161          *  @param length length of arary
162          *  @param objectSize size of object in array
163          *  @return size of array
164          */
165         public static long getArraySize(int length, long objectSize) {
166                 return getArrayExtraSize() + length * objectSize;
167         }
168 
169         /**
170          *  Calculate approximate size of biggest of MemoryObjects.
171          */
172         public static long getMemoryObjectSize(long size) {
173                 return size + 2 * getReferenceSize() + getObjectExtraSize();
174         }
175 

  1 /*
  2  * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 package nsk.share.gc;
 25 
 26 import nsk.share.test.LocalRandom;
 27 import java.io.PrintStream;
 28 import nsk.share.gc.gp.GarbageProducer;
 29 import nsk.share.gc.tree.*;

 30 import nsk.share.log.Log;
 31 
 32 /**
 33  *  Different utility methods to work with memory objects.
 34  */
 35 public final class Memory {
 36         private static int bits = 0;
 37         private static int referenceSize = 0;
 38         private static int objectExtraSize = 0;
 39 
 40         private static final boolean previewEnabled =
 41                 System.getProperty("test.java.opts", "").contains("--enable-preview") ||
 42                 System.getProperty("test.vm.opts", "").contains("--enable-preview");
 43 
 44 
 45         private Memory() {
 46         }
 47 
 48         private static int getBits() {
 49                 if (bits == 0)
 50                         bits = Integer.parseInt(System.getProperty("sun.arch.data.model"));
 51                 return bits;
 52         }
 53 
 54         /**
 55          *  Get size of one object reference.
 56          *
 57          *  TODO: somehow determine the real value
 58          */
 59         public static int getReferenceSize() {
 60                 if (referenceSize == 0)
 61                         referenceSize = (getBits() == 64) ? 8 : 4;
 62                 return referenceSize;
 63         }
 64 
 65         public static boolean isPreviewEnabled() {
 66                 return previewEnabled;
 67         }
 68 
 69         public static boolean isValhallaEnabled() {
 70             return previewEnabled;
 71         }
 72 
 73         /**
 74          * Get size of primitive type int.
 75          */
 76         public static int getIntSize() {
 77                 return 4;
 78         }
 79 
 80         /**
 81          * Get size of primitive type boolean.
 82          */
 83         public static int getBooleanSize() {
 84                 return 1;
 85         }
 86 
 87         /**
 88          * Get size of primitive type byte.
 89          */
 90         public static int getByteSize() {
 91                 return 1;
 92         }

110          */
111         public static int getLongSize() {
112                 return 8;
113         }
114 
115         /**
116          * Get size of primitive type float.
117          */
118         public static int getFloatSize() {
119                 return 4;
120         }
121 
122         /**
123          * Get size of primitive type double.
124          */
125         public static int getDoubleSize() {
126                 return 8;
127         }
128 
129         /**
130          * Get size of Integer element in Integer array.
131          * If array is not flattened, the only reference size is returned, the referred values
132          * should be added manually in references are not null.
133          */
134         public static int getIntegerArrayElementSize() {
135 
136             if (!Memory.isValhallaEnabled()) {
137                 return getReferenceSize();
138             }
139             return 8;
140         }
141 
142         /**
143          * Get size of Byte element in Byte array.
144          * If array is not flattened, the only reference size is returned, the referred values
145          * should be added manually in references are not null.
146          */
147         public static int getByteArrayElementSize() {
148 
149             if (!Memory.isValhallaEnabled()) {
150                 return getReferenceSize();
151             }
152             return 2;
153         }
154 
155         /**
156          * Get size of Boolean element in Boolean array.
157          * If array is not flattened, the only reference size is returned, the referred values
158          * should be added manually in references are not null.
159          */
160         public static int getBooleanArrayElementSize() {
161             if (!Memory.isValhallaEnabled()) {
162                 return getReferenceSize();
163             }
164             return 2;
165         }
166 
167 
168 
169     /**
170          *  Get how many extra bytes an object occupies in the heap
171          *  compared to sum of it's fields.
172          *
173          *  TODO: somehow determine the real value
174          */
175         public static int getObjectExtraSize() {
176                 if (objectExtraSize == 0)
177                         objectExtraSize = 2 * getReferenceSize();
178                 return objectExtraSize;
179         }
180         /**
181          *  Get how many extra bytes an array occupies in the heap
182          *  compared to sum of it's fields.
183          *
184          *  TODO: somehow determine the real value
185          */
186         public static int getArrayExtraSize() {
187                 return getObjectExtraSize();
188         }
189 
190         /**
191          * Return size of reference object (SoftReference, WeakReference, PhantomReference)
192          */
193         public static int getReferenceObjectSize() {
194                 return getReferenceSize() + getObjectExtraSize();
195         }
196 
197         /**
198          *  Get an approximate length of array that will occupy a given memory.
199          *
200          *  @param memory size of memory
201          *  @param objectSize size of each object in array
202          *  @return length of array
203          */
204         public static int getArrayLength(long memory, long objectSize) {
205                 int arrayExtraSize = getArrayExtraSize();
206                 int length = (int) Math.min((memory - arrayExtraSize) / objectSize,
207                         Integer.MAX_VALUE);
208                 return Math.max(length, 0);
209         }
210 
211         /**
212          *  Get an approximate size of array of given length and object size.
213          *
214          *  @param length length of arary
215          *  @param objectSize size of object in array
216          *  @return size of array
217          */
218         public static long getArraySize(int length, long objectSize) {
219                 return getArrayExtraSize() + length * objectSize;
220         }
221 
222         /**
223          *  Calculate approximate size of biggest of MemoryObjects.
224          */
225         public static long getMemoryObjectSize(long size) {
226                 return size + 2 * getReferenceSize() + getObjectExtraSize();
227         }
228 
< prev index next >