Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [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. |
| 23 | * |
| 24 | * @hide |
| 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 | 1ec5a34 | 2013-10-24 16:30:14 -0700 | [diff] [blame] | 33 | private int targetSdkVersion; |
| 34 | |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [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 | /** |
| 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 | /** |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [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 | * |
| 93 | * <p>This is only a hint to the garbage collector and may be ignored. |
| 94 | * |
| 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) { |
| 101 | if (newTarget <= 0.0f || newTarget >= 1.0f) { |
| 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 | /** |
| 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 |
| 119 | * dangerous ways. Use 0 to mean "current" (since callers won't |
| 120 | * necessarily know the actual current SDK version, and the |
Alex Klyubin | 1ec5a34 | 2013-10-24 16:30:14 -0700 | [diff] [blame] | 121 | * allocated version numbers start at 1), and 10000 to mean |
| 122 | * CUR_DEVELOPMENT. |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 123 | */ |
Alex Klyubin | 1ec5a34 | 2013-10-24 16:30:14 -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); |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
| 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). |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 142 | */ |
| 143 | @Deprecated |
| 144 | public long getMinimumHeapSize() { |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /** |
| 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). |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 151 | */ |
| 152 | @Deprecated |
| 153 | public long setMinimumHeapSize(long size) { |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * This method exists for binary compatibility. It used to |
| 159 | * perform a garbage collection that cleared SoftReferences. |
| 160 | */ |
| 161 | @Deprecated |
| 162 | public void gcSoftReferences() {} |
| 163 | |
| 164 | /** |
| 165 | * This method exists for binary compatibility. It is equivalent |
| 166 | * to {@link System#runFinalization}. |
| 167 | */ |
| 168 | @Deprecated |
| 169 | public void runFinalizationSync() { |
| 170 | System.runFinalization(); |
| 171 | } |
| 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 | /** |
| 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). |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 184 | */ |
| 185 | @Deprecated |
| 186 | public boolean trackExternalAllocation(long size) { |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | /** |
| 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). |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 193 | */ |
| 194 | @Deprecated |
| 195 | public void trackExternalFree(long size) {} |
| 196 | |
| 197 | /** |
| 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). |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 200 | */ |
| 201 | @Deprecated |
| 202 | public long getExternalBytesAllocated() { |
| 203 | return 0; |
| 204 | } |
| 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. |
| 209 | */ |
| 210 | public native void startJitCompilation(); |
| 211 | |
| 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. |
| 215 | */ |
| 216 | public native void disableJitCompilation(); |
| 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); |
| 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(); |
| 236 | |
| 237 | /** |
| 238 | * Returns true if either a Java debugger or native debugger is active. |
| 239 | */ |
| 240 | public native boolean isDebuggerActive(); |
| 241 | |
Mathieu Chartier | ddd0748 | 2013-07-10 13:33:33 -0700 | [diff] [blame] | 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); |
| 256 | |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 257 | public native void trimHeap(); |
| 258 | public native void concurrentGC(); |
Brian Carlstrom | c8cfc66 | 2013-10-07 17:47:45 -0700 | [diff] [blame] | 259 | |
Brian Carlstrom | 65e6aca | 2013-10-15 21:55:03 -0700 | [diff] [blame] | 260 | /** |
Mathieu Chartier | 0c85c33 | 2013-11-25 14:26:22 -0800 | [diff] [blame] | 261 | * Let the heap know of the new process state. This can change allocation and garbage collection |
| 262 | * behavior regarding trimming and compaction. |
| 263 | */ |
| 264 | public native void updateProcessState(int state); |
| 265 | |
| 266 | /** |
Brian Carlstrom | 65e6aca | 2013-10-15 21:55:03 -0700 | [diff] [blame] | 267 | * Fill in dex caches with classes, fields, and methods that are |
| 268 | * already loaded. Typically used after Zygote preloading. |
| 269 | */ |
| 270 | public native void preloadDexCaches(); |
Dave Allison | eee45fe | 2013-11-13 17:17:45 -0800 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * Register application info |
| 274 | */ |
| 275 | public static native void registerAppInfo(String appDir, String processName); |
Brian Carlstrom | e96f94f | 2013-06-04 23:54:52 -0700 | [diff] [blame] | 276 | } |