The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package dalvik.system; |
| 18 | |
| 19 | /** |
| 20 | * Provides an interface to VM-global, Dalvik-specific features. |
| 21 | * An application cannot create its own Runtime instance, and must obtain |
| 22 | * one from the getRuntime method. |
Jesse Wilson | 04aaaf1 | 2009-09-22 23:42:13 -0700 | [diff] [blame] | 23 | * |
Jesse Wilson | 8f27334 | 2010-04-01 15:44:05 -0700 | [diff] [blame] | 24 | * @hide |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | */ |
| 26 | public final class VMRuntime { |
| 27 | |
| 28 | /** |
| 29 | * Holds the VMRuntime singleton. |
| 30 | */ |
| 31 | private static final VMRuntime THE_ONE = new VMRuntime(); |
| 32 | |
Alex Klyubin | 7ec3dc1 | 2013-10-24 15:12:44 -0700 | [diff] [blame] | 33 | private int targetSdkVersion; |
| 34 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Prevents this class from being instantiated. |
| 37 | */ |
| 38 | private VMRuntime() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the object that represents the VM instance's Dalvik-specific |
| 43 | * runtime environment. |
| 44 | * |
| 45 | * @return the runtime object |
| 46 | */ |
| 47 | public static VMRuntime getRuntime() { |
| 48 | return THE_ONE; |
| 49 | } |
| 50 | |
| 51 | /** |
Elliott Hughes | 24923f9 | 2011-02-10 12:01:34 -0800 | [diff] [blame] | 52 | * Returns a copy of the VM's command-line property settings. |
| 53 | * These are in the form "name=value" rather than "-Dname=value". |
| 54 | */ |
| 55 | public native String[] properties(); |
| 56 | |
| 57 | /** |
| 58 | * Returns the VM's boot class path. |
| 59 | */ |
| 60 | public native String bootClassPath(); |
| 61 | |
| 62 | /** |
| 63 | * Returns the VM's class path. |
| 64 | */ |
| 65 | public native String classPath(); |
| 66 | |
| 67 | /** |
| 68 | * Returns the VM's version. |
| 69 | */ |
| 70 | public native String vmVersion(); |
| 71 | |
| 72 | /** |
Brian Carlstrom | 2637676 | 2013-06-28 14:33:39 -0700 | [diff] [blame] | 73 | * Returns the name of the shared library providing the VM implementation. |
| 74 | */ |
| 75 | public native String vmLibrary(); |
| 76 | |
| 77 | /** |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 78 | * Gets the current ideal heap utilization, represented as a number |
| 79 | * between zero and one. After a GC happens, the Dalvik heap may |
| 80 | * be resized so that (size of live objects) / (size of heap) is |
| 81 | * equal to this number. |
| 82 | * |
| 83 | * @return the current ideal heap utilization |
| 84 | */ |
| 85 | public native float getTargetHeapUtilization(); |
| 86 | |
| 87 | /** |
| 88 | * Sets the current ideal heap utilization, represented as a number |
| 89 | * between zero and one. After a GC happens, the Dalvik heap may |
| 90 | * be resized so that (size of live objects) / (size of heap) is |
| 91 | * equal to this number. |
| 92 | * |
Jesse Wilson | 04aaaf1 | 2009-09-22 23:42:13 -0700 | [diff] [blame] | 93 | * <p>This is only a hint to the garbage collector and may be ignored. |
| 94 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 95 | * @param newTarget the new suggested ideal heap utilization. |
| 96 | * This value may be adjusted internally. |
| 97 | * @return the previous ideal heap utilization |
| 98 | * @throws IllegalArgumentException if newTarget is <= 0.0 or >= 1.0 |
| 99 | */ |
| 100 | public float setTargetHeapUtilization(float newTarget) { |
Ian Rogers | 63ac769 | 2012-03-20 10:51:03 -0700 | [diff] [blame] | 101 | if (newTarget <= 0.0f || newTarget >= 1.0f) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 102 | throw new IllegalArgumentException(newTarget + |
| 103 | " out of range (0,1)"); |
| 104 | } |
| 105 | /* Synchronize to make sure that only one thread gets |
| 106 | * a given "old" value if both update at the same time. |
| 107 | * Allows for reliable save-and-restore semantics. |
| 108 | */ |
| 109 | synchronized (this) { |
| 110 | float oldTarget = getTargetHeapUtilization(); |
| 111 | nativeSetTargetHeapUtilization(newTarget); |
| 112 | return oldTarget; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
Elliott Hughes | 675351b | 2011-07-08 11:11:26 -0700 | [diff] [blame] | 117 | * Sets the target SDK version. Should only be called before the |
| 118 | * app starts to run, because it may change the VM's behavior in |
Elliott Hughes | 4ee53d1 | 2011-07-10 17:59:33 -0700 | [diff] [blame] | 119 | * dangerous ways. Use 0 to mean "current" (since callers won't |
| 120 | * necessarily know the actual current SDK version, and the |
Alex Klyubin | 7ec3dc1 | 2013-10-24 15:12:44 -0700 | [diff] [blame] | 121 | * allocated version numbers start at 1), and 10000 to mean |
| 122 | * CUR_DEVELOPMENT. |
Elliott Hughes | 675351b | 2011-07-08 11:11:26 -0700 | [diff] [blame] | 123 | */ |
Alex Klyubin | 7ec3dc1 | 2013-10-24 15:12:44 -0700 | [diff] [blame] | 124 | public synchronized void setTargetSdkVersion(int targetSdkVersion) { |
| 125 | this.targetSdkVersion = targetSdkVersion; |
| 126 | setTargetSdkVersionNative(this.targetSdkVersion); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Gets the target SDK version. See {@link #setTargetSdkVersion} for |
| 131 | * special values. |
| 132 | */ |
| 133 | public synchronized int getTargetSdkVersion() { |
| 134 | return targetSdkVersion; |
| 135 | } |
| 136 | |
| 137 | private native void setTargetSdkVersionNative(int targetSdkVersion); |
Elliott Hughes | 675351b | 2011-07-08 11:11:26 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
Carl Shapiro | cb966ae | 2011-01-12 13:26:51 -0800 | [diff] [blame] | 140 | * This method exists for binary compatibility. It was part of a |
Elliott Hughes | 413d4592 | 2013-09-03 13:32:52 -0700 | [diff] [blame] | 141 | * heap sizing API which was removed in Android 3.0 (Honeycomb). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 142 | */ |
Carl Shapiro | cb966ae | 2011-01-12 13:26:51 -0800 | [diff] [blame] | 143 | @Deprecated |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 144 | public long getMinimumHeapSize() { |
Carl Shapiro | cb966ae | 2011-01-12 13:26:51 -0800 | [diff] [blame] | 145 | return 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
Carl Shapiro | cb966ae | 2011-01-12 13:26:51 -0800 | [diff] [blame] | 149 | * This method exists for binary compatibility. It was part of a |
Elliott Hughes | 413d4592 | 2013-09-03 13:32:52 -0700 | [diff] [blame] | 150 | * heap sizing API which was removed in Android 3.0 (Honeycomb). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 151 | */ |
Carl Shapiro | cb966ae | 2011-01-12 13:26:51 -0800 | [diff] [blame] | 152 | @Deprecated |
| 153 | public long setMinimumHeapSize(long size) { |
| 154 | return 0; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
Carl Shapiro | 3b7c2eb | 2011-02-07 21:04:53 -0800 | [diff] [blame] | 158 | * This method exists for binary compatibility. It used to |
| 159 | * perform a garbage collection that cleared SoftReferences. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 160 | */ |
Carl Shapiro | 3b7c2eb | 2011-02-07 21:04:53 -0800 | [diff] [blame] | 161 | @Deprecated |
| 162 | public void gcSoftReferences() {} |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 163 | |
| 164 | /** |
Carl Shapiro | bbfadc8 | 2011-03-21 13:36:30 -0700 | [diff] [blame] | 165 | * This method exists for binary compatibility. It is equivalent |
| 166 | * to {@link System#runFinalization}. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 167 | */ |
Carl Shapiro | bbfadc8 | 2011-03-21 13:36:30 -0700 | [diff] [blame] | 168 | @Deprecated |
| 169 | public void runFinalizationSync() { |
| 170 | System.runFinalization(); |
| 171 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Implements setTargetHeapUtilization(). |
| 175 | * |
| 176 | * @param newTarget the new suggested ideal heap utilization. |
| 177 | * This value may be adjusted internally. |
| 178 | */ |
| 179 | private native void nativeSetTargetHeapUtilization(float newTarget); |
| 180 | |
| 181 | /** |
Carl Shapiro | 1f7938c | 2010-12-17 16:19:57 -0800 | [diff] [blame] | 182 | * This method exists for binary compatibility. It was part of |
Elliott Hughes | 413d4592 | 2013-09-03 13:32:52 -0700 | [diff] [blame] | 183 | * the external allocation API which was removed in Android 3.0 (Honeycomb). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 184 | */ |
Andy McFadden | cd25643 | 2010-03-31 15:50:17 -0700 | [diff] [blame] | 185 | @Deprecated |
Carl Shapiro | 1f7938c | 2010-12-17 16:19:57 -0800 | [diff] [blame] | 186 | public boolean trackExternalAllocation(long size) { |
| 187 | return true; |
| 188 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 189 | |
| 190 | /** |
Carl Shapiro | 1f7938c | 2010-12-17 16:19:57 -0800 | [diff] [blame] | 191 | * This method exists for binary compatibility. It was part of |
Elliott Hughes | 413d4592 | 2013-09-03 13:32:52 -0700 | [diff] [blame] | 192 | * the external allocation API which was removed in Android 3.0 (Honeycomb). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 193 | */ |
Andy McFadden | cd25643 | 2010-03-31 15:50:17 -0700 | [diff] [blame] | 194 | @Deprecated |
Carl Shapiro | 1f7938c | 2010-12-17 16:19:57 -0800 | [diff] [blame] | 195 | public void trackExternalFree(long size) {} |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 196 | |
| 197 | /** |
Carl Shapiro | 1f7938c | 2010-12-17 16:19:57 -0800 | [diff] [blame] | 198 | * This method exists for binary compatibility. It was part of |
Elliott Hughes | 413d4592 | 2013-09-03 13:32:52 -0700 | [diff] [blame] | 199 | * the external allocation API which was removed in Android 3.0 (Honeycomb). |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 200 | */ |
Andy McFadden | cd25643 | 2010-03-31 15:50:17 -0700 | [diff] [blame] | 201 | @Deprecated |
Carl Shapiro | 1f7938c | 2010-12-17 16:19:57 -0800 | [diff] [blame] | 202 | public long getExternalBytesAllocated() { |
| 203 | return 0; |
| 204 | } |
Ben Cheng | 4326376 | 2010-01-28 13:56:52 -0800 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Tells the VM to enable the JIT compiler. If the VM does not have a JIT |
| 208 | * implementation, calling this method should have no effect. |
Ben Cheng | 4326376 | 2010-01-28 13:56:52 -0800 | [diff] [blame] | 209 | */ |
Ben Cheng | 5a7d0bc | 2010-01-28 16:47:03 -0800 | [diff] [blame] | 210 | public native void startJitCompilation(); |
Ben Cheng | 4326376 | 2010-01-28 13:56:52 -0800 | [diff] [blame] | 211 | |
Ben Cheng | b804353 | 2010-02-14 16:17:36 -0800 | [diff] [blame] | 212 | /** |
| 213 | * Tells the VM to disable the JIT compiler. If the VM does not have a JIT |
| 214 | * implementation, calling this method should have no effect. |
Ben Cheng | b804353 | 2010-02-14 16:17:36 -0800 | [diff] [blame] | 215 | */ |
| 216 | public native void disableJitCompilation(); |
Elliott Hughes | 3676bd2 | 2011-01-10 15:35:18 -0800 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * Returns an array allocated in an area of the Java heap where it will never be moved. |
| 220 | * This is used to implement native allocations on the Java heap, such as DirectByteBuffers |
| 221 | * and Bitmaps. |
| 222 | */ |
| 223 | public native Object newNonMovableArray(Class<?> componentType, int length); |
| 224 | |
| 225 | /** |
| 226 | * Returns the address of array[0]. This differs from using JNI in that JNI might lie and |
| 227 | * give you the address of a copy of the array when in forcecopy mode. |
| 228 | */ |
| 229 | public native long addressOf(Object array); |
Carl Shapiro | 2793254 | 2011-01-18 18:02:46 -0800 | [diff] [blame] | 230 | |
| 231 | /** |
| 232 | * Removes any growth limits, allowing the application to allocate |
| 233 | * up to the maximum heap size. |
| 234 | */ |
| 235 | public native void clearGrowthLimit(); |
Jesse Wilson | 64c6c36 | 2011-06-20 15:08:30 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Returns true if either a Java debugger or native debugger is active. |
| 239 | */ |
| 240 | public native boolean isDebuggerActive(); |
Mathieu Chartier | 7490f25 | 2013-07-16 11:54:13 -0700 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Registers a native allocation so that the heap knows about it and performs GC as required. |
| 244 | * If the number of native allocated bytes exceeds the native allocation watermark, the |
| 245 | * function requests a concurrent GC. If the native bytes allocated exceeds a second higher |
| 246 | * watermark, it is determined that the application is registering native allocations at an |
| 247 | * unusually high rate and a GC is performed inside of the function to prevent memory usage |
| 248 | * from excessively increasing. |
| 249 | */ |
| 250 | public native void registerNativeAllocation(int bytes); |
| 251 | |
| 252 | /** |
| 253 | * Registers a native free by reducing the number of native bytes accounted for. |
| 254 | */ |
| 255 | public native void registerNativeFree(int bytes); |
Brian Carlstrom | c8cfc66 | 2013-10-07 17:47:45 -0700 | [diff] [blame] | 256 | |
| 257 | /** |
Mathieu Chartier | 0c85c33 | 2013-11-25 14:26:22 -0800 | [diff] [blame] | 258 | * Let the heap know of the new process state. This can change allocation and garbage collection |
| 259 | * behavior regarding trimming and compaction. |
| 260 | */ |
| 261 | public native void updateProcessState(int state); |
| 262 | |
| 263 | /** |
Brian Carlstrom | 65e6aca | 2013-10-15 21:55:03 -0700 | [diff] [blame] | 264 | * Fill in dex caches with classes, fields, and methods that are |
| 265 | * already loaded. Typically used after Zygote preloading. |
Brian Carlstrom | c8cfc66 | 2013-10-07 17:47:45 -0700 | [diff] [blame] | 266 | */ |
| 267 | public native void preloadDexCaches(); |
Dave Allison | eee45fe | 2013-11-13 17:17:45 -0800 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * Register application info |
| 271 | */ |
| 272 | public static void registerAppInfo(String appDir, String processName) { |
| 273 | // Nothing to do in dalvik. |
| 274 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 275 | } |