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 33 /** 34 * Prevents this class from being instantiated. 35 */ 36 private VMRuntime() { 37 } 38 39 /** 40 * Returns the object that represents the VM instance's Dalvik-specific 41 * runtime environment. 42 * 43 * @return the runtime object 44 */ 45 public static VMRuntime getRuntime() { 46 return THE_ONE; 47 } 48 49 /** 50 * Returns a copy of the VM's command-line property settings. 51 * These are in the form "name=value" rather than "-Dname=value". 52 */ 53 public native String[] properties(); 54 55 /** 56 * Returns the VM's boot class path. 57 */ 58 public native String bootClassPath(); 59 60 /** 61 * Returns the VM's class path. 62 */ 63 public native String classPath(); 64 65 /** 66 * Returns the VM's version. 67 */ 68 public native String vmVersion(); 69 70 /** 71 * Gets the current ideal heap utilization, represented as a number 72 * between zero and one. After a GC happens, the Dalvik heap may 73 * be resized so that (size of live objects) / (size of heap) is 74 * equal to this number. 75 * 76 * @return the current ideal heap utilization 77 */ 78 public native float getTargetHeapUtilization(); 79 80 /** 81 * Sets the current ideal heap utilization, represented as a number 82 * between zero and one. After a GC happens, the Dalvik heap may 83 * be resized so that (size of live objects) / (size of heap) is 84 * equal to this number. 85 * 86 * <p>This is only a hint to the garbage collector and may be ignored. 87 * 88 * @param newTarget the new suggested ideal heap utilization. 89 * This value may be adjusted internally. 90 * @return the previous ideal heap utilization 91 * @throws IllegalArgumentException if newTarget is <= 0.0 or >= 1.0 92 */ 93 public float setTargetHeapUtilization(float newTarget) { 94 if (newTarget <= 0.0 || newTarget >= 1.0) { 95 throw new IllegalArgumentException(newTarget + 96 " out of range (0,1)"); 97 } 98 /* Synchronize to make sure that only one thread gets 99 * a given "old" value if both update at the same time. 100 * Allows for reliable save-and-restore semantics. 101 */ 102 synchronized (this) { 103 float oldTarget = getTargetHeapUtilization(); 104 nativeSetTargetHeapUtilization(newTarget); 105 return oldTarget; 106 } 107 } 108 109 /** 110 * Sets the target SDK version. Should only be called before the 111 * app starts to run, because it may change the VM's behavior in 112 * dangerous ways. Use 0 to mean "current" (since callers won't 113 * necessarily know the actual current SDK version, and the 114 * allocated version numbers start at 1). 115 */ 116 public native void setTargetSdkVersion(int targetSdkVersion); 117 118 /** 119 * This method exists for binary compatibility. It was part of a 120 * heap sizing API which was removed in Honeycomb. 121 */ 122 @Deprecated 123 public long getMinimumHeapSize() { 124 return 0; 125 } 126 127 /** 128 * This method exists for binary compatibility. It was part of a 129 * heap sizing API which was removed in Honeycomb. 130 */ 131 @Deprecated 132 public long setMinimumHeapSize(long size) { 133 return 0; 134 } 135 136 /** 137 * This method exists for binary compatibility. It used to 138 * perform a garbage collection that cleared SoftReferences. 139 */ 140 @Deprecated 141 public void gcSoftReferences() {} 142 143 /** 144 * This method exists for binary compatibility. It is equivalent 145 * to {@link System#runFinalization}. 146 */ 147 @Deprecated 148 public void runFinalizationSync() { 149 System.runFinalization(); 150 } 151 152 /** 153 * Implements setTargetHeapUtilization(). 154 * 155 * @param newTarget the new suggested ideal heap utilization. 156 * This value may be adjusted internally. 157 */ 158 private native void nativeSetTargetHeapUtilization(float newTarget); 159 160 /** 161 * This method exists for binary compatibility. It was part of 162 * the external allocation API which was removed in Honeycomb. 163 */ 164 @Deprecated 165 public boolean trackExternalAllocation(long size) { 166 return true; 167 } 168 169 /** 170 * This method exists for binary compatibility. It was part of 171 * the external allocation API which was removed in Honeycomb. 172 */ 173 @Deprecated 174 public void trackExternalFree(long size) {} 175 176 /** 177 * This method exists for binary compatibility. It was part of 178 * the external allocation API which was removed in Honeycomb. 179 */ 180 @Deprecated 181 public long getExternalBytesAllocated() { 182 return 0; 183 } 184 185 /** 186 * Tells the VM to enable the JIT compiler. If the VM does not have a JIT 187 * implementation, calling this method should have no effect. 188 */ 189 public native void startJitCompilation(); 190 191 /** 192 * Tells the VM to disable the JIT compiler. If the VM does not have a JIT 193 * implementation, calling this method should have no effect. 194 */ 195 public native void disableJitCompilation(); 196 197 /** 198 * Returns an array allocated in an area of the Java heap where it will never be moved. 199 * This is used to implement native allocations on the Java heap, such as DirectByteBuffers 200 * and Bitmaps. 201 */ 202 public native Object newNonMovableArray(Class<?> componentType, int length); 203 204 /** 205 * Returns the address of array[0]. This differs from using JNI in that JNI might lie and 206 * give you the address of a copy of the array when in forcecopy mode. 207 */ 208 public native long addressOf(Object array); 209 210 /** 211 * Removes any growth limits, allowing the application to allocate 212 * up to the maximum heap size. 213 */ 214 public native void clearGrowthLimit(); 215 216 /** 217 * Returns true if either a Java debugger or native debugger is active. 218 */ 219 public native boolean isDebuggerActive(); 220 } 221