blob: f4acf476318533353e8b66dd0273e35888a840d7 [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
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
17package 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 Wilson04aaaf12009-09-22 23:42:13 -070023 *
Jesse Wilson8f273342010-04-01 15:44:05 -070024 * @hide
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080025 */
26public final class VMRuntime {
27
28 /**
29 * Holds the VMRuntime singleton.
30 */
31 private static final VMRuntime THE_ONE = new VMRuntime();
32
Alex Klyubin7ec3dc12013-10-24 15:12:44 -070033 private int targetSdkVersion;
34
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080035 /**
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 Hughes24923f92011-02-10 12:01:34 -080052 * 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 Carlstrom26376762013-06-28 14:33:39 -070073 * Returns the name of the shared library providing the VM implementation.
74 */
75 public native String vmLibrary();
76
77 /**
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080078 * 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 Wilson04aaaf12009-09-22 23:42:13 -070093 * <p>This is only a hint to the garbage collector and may be ignored.
94 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080095 * @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 &lt;= 0.0 or &gt;= 1.0
99 */
100 public float setTargetHeapUtilization(float newTarget) {
Ian Rogers63ac7692012-03-20 10:51:03 -0700101 if (newTarget <= 0.0f || newTarget >= 1.0f) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800102 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 Hughes675351b2011-07-08 11:11:26 -0700117 * 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 Hughes4ee53d12011-07-10 17:59:33 -0700119 * dangerous ways. Use 0 to mean "current" (since callers won't
120 * necessarily know the actual current SDK version, and the
Alex Klyubin7ec3dc12013-10-24 15:12:44 -0700121 * allocated version numbers start at 1), and 10000 to mean
122 * CUR_DEVELOPMENT.
Elliott Hughes675351b2011-07-08 11:11:26 -0700123 */
Alex Klyubin7ec3dc12013-10-24 15:12:44 -0700124 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 Hughes675351b2011-07-08 11:11:26 -0700138
139 /**
Carl Shapirocb966ae2011-01-12 13:26:51 -0800140 * This method exists for binary compatibility. It was part of a
Elliott Hughes413d45922013-09-03 13:32:52 -0700141 * heap sizing API which was removed in Android 3.0 (Honeycomb).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800142 */
Carl Shapirocb966ae2011-01-12 13:26:51 -0800143 @Deprecated
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800144 public long getMinimumHeapSize() {
Carl Shapirocb966ae2011-01-12 13:26:51 -0800145 return 0;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800146 }
147
148 /**
Carl Shapirocb966ae2011-01-12 13:26:51 -0800149 * This method exists for binary compatibility. It was part of a
Elliott Hughes413d45922013-09-03 13:32:52 -0700150 * heap sizing API which was removed in Android 3.0 (Honeycomb).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800151 */
Carl Shapirocb966ae2011-01-12 13:26:51 -0800152 @Deprecated
153 public long setMinimumHeapSize(long size) {
154 return 0;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800155 }
156
157 /**
Carl Shapiro3b7c2eb2011-02-07 21:04:53 -0800158 * This method exists for binary compatibility. It used to
159 * perform a garbage collection that cleared SoftReferences.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800160 */
Carl Shapiro3b7c2eb2011-02-07 21:04:53 -0800161 @Deprecated
162 public void gcSoftReferences() {}
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800163
164 /**
Carl Shapirobbfadc82011-03-21 13:36:30 -0700165 * This method exists for binary compatibility. It is equivalent
166 * to {@link System#runFinalization}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800167 */
Carl Shapirobbfadc82011-03-21 13:36:30 -0700168 @Deprecated
169 public void runFinalizationSync() {
170 System.runFinalization();
171 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800172
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 Shapiro1f7938c2010-12-17 16:19:57 -0800182 * This method exists for binary compatibility. It was part of
Elliott Hughes413d45922013-09-03 13:32:52 -0700183 * the external allocation API which was removed in Android 3.0 (Honeycomb).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800184 */
Andy McFaddencd256432010-03-31 15:50:17 -0700185 @Deprecated
Carl Shapiro1f7938c2010-12-17 16:19:57 -0800186 public boolean trackExternalAllocation(long size) {
187 return true;
188 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800189
190 /**
Carl Shapiro1f7938c2010-12-17 16:19:57 -0800191 * This method exists for binary compatibility. It was part of
Elliott Hughes413d45922013-09-03 13:32:52 -0700192 * the external allocation API which was removed in Android 3.0 (Honeycomb).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800193 */
Andy McFaddencd256432010-03-31 15:50:17 -0700194 @Deprecated
Carl Shapiro1f7938c2010-12-17 16:19:57 -0800195 public void trackExternalFree(long size) {}
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800196
197 /**
Carl Shapiro1f7938c2010-12-17 16:19:57 -0800198 * This method exists for binary compatibility. It was part of
Elliott Hughes413d45922013-09-03 13:32:52 -0700199 * the external allocation API which was removed in Android 3.0 (Honeycomb).
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800200 */
Andy McFaddencd256432010-03-31 15:50:17 -0700201 @Deprecated
Carl Shapiro1f7938c2010-12-17 16:19:57 -0800202 public long getExternalBytesAllocated() {
203 return 0;
204 }
Ben Cheng43263762010-01-28 13:56:52 -0800205
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 Cheng43263762010-01-28 13:56:52 -0800209 */
Ben Cheng5a7d0bc2010-01-28 16:47:03 -0800210 public native void startJitCompilation();
Ben Cheng43263762010-01-28 13:56:52 -0800211
Ben Chengb8043532010-02-14 16:17:36 -0800212 /**
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 Chengb8043532010-02-14 16:17:36 -0800215 */
216 public native void disableJitCompilation();
Elliott Hughes3676bd22011-01-10 15:35:18 -0800217
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 Shapiro27932542011-01-18 18:02:46 -0800230
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 Wilson64c6c362011-06-20 15:08:30 -0700236
237 /**
238 * Returns true if either a Java debugger or native debugger is active.
239 */
240 public native boolean isDebuggerActive();
Mathieu Chartier7490f252013-07-16 11:54:13 -0700241
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 Carlstromc8cfc662013-10-07 17:47:45 -0700256
257 /**
Mathieu Chartier0c85c332013-11-25 14:26:22 -0800258 * 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 Carlstrom65e6aca2013-10-15 21:55:03 -0700264 * Fill in dex caches with classes, fields, and methods that are
265 * already loaded. Typically used after Zygote preloading.
Brian Carlstromc8cfc662013-10-07 17:47:45 -0700266 */
267 public native void preloadDexCaches();
Dave Allisoneee45fe2013-11-13 17:17:45 -0800268
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 Projectadc854b2009-03-03 19:28:47 -0800275}