Search in sources :

Example 1 with VMRuntime

use of dalvik.system.VMRuntime in project android_frameworks_base by ParanoidAndroid.

the class ZygoteInit method gc.

/**
     * Runs several special GCs to try to clean up a few generations of
     * softly- and final-reachable objects, along with any other garbage.
     * This is only useful just before a fork().
     */
/*package*/
static void gc() {
    final VMRuntime runtime = VMRuntime.getRuntime();
    /* runFinalizationSync() lets finalizers be called in Zygote,
         * which doesn't have a HeapWorker thread.
         */
    System.gc();
    runtime.runFinalizationSync();
    System.gc();
    runtime.runFinalizationSync();
    System.gc();
    runtime.runFinalizationSync();
}
Also used : VMRuntime(dalvik.system.VMRuntime)

Example 2 with VMRuntime

use of dalvik.system.VMRuntime in project android_frameworks_base by ParanoidAndroid.

the class ZygoteInit method preloadClasses.

/**
     * Performs Zygote process initialization. Loads and initializes
     * commonly used classes.
     *
     * Most classes only cause a few hundred bytes to be allocated, but
     * a few will allocate a dozen Kbytes (in one case, 500+K).
     */
private static void preloadClasses() {
    final VMRuntime runtime = VMRuntime.getRuntime();
    InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(PRELOADED_CLASSES);
    if (is == null) {
        Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + ".");
    } else {
        Log.i(TAG, "Preloading classes...");
        long startTime = SystemClock.uptimeMillis();
        // Drop root perms while running static initializers.
        setEffectiveGroup(UNPRIVILEGED_GID);
        setEffectiveUser(UNPRIVILEGED_UID);
        // Alter the target heap utilization.  With explicit GCs this
        // is not likely to have any effect.
        float defaultUtilization = runtime.getTargetHeapUtilization();
        runtime.setTargetHeapUtilization(0.8f);
        // Start with a clean slate.
        System.gc();
        runtime.runFinalizationSync();
        Debug.startAllocCounting();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is), 256);
            int count = 0;
            String line;
            while ((line = br.readLine()) != null) {
                // Skip comments and blank lines.
                line = line.trim();
                if (line.startsWith("#") || line.equals("")) {
                    continue;
                }
                try {
                    if (false) {
                        Log.v(TAG, "Preloading " + line + "...");
                    }
                    Class.forName(line);
                    if (Debug.getGlobalAllocSize() > PRELOAD_GC_THRESHOLD) {
                        if (false) {
                            Log.v(TAG, " GC at " + Debug.getGlobalAllocSize());
                        }
                        System.gc();
                        runtime.runFinalizationSync();
                        Debug.resetGlobalAllocSize();
                    }
                    count++;
                } catch (ClassNotFoundException e) {
                    Log.w(TAG, "Class not found for preloading: " + line);
                } catch (Throwable t) {
                    Log.e(TAG, "Error preloading " + line + ".", t);
                    if (t instanceof Error) {
                        throw (Error) t;
                    }
                    if (t instanceof RuntimeException) {
                        throw (RuntimeException) t;
                    }
                    throw new RuntimeException(t);
                }
            }
            Log.i(TAG, "...preloaded " + count + " classes in " + (SystemClock.uptimeMillis() - startTime) + "ms.");
        } catch (IOException e) {
            Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e);
        } finally {
            IoUtils.closeQuietly(is);
            // Restore default.
            runtime.setTargetHeapUtilization(defaultUtilization);
            Debug.stopAllocCounting();
            // Bring back root. We'll need it later.
            setEffectiveUser(ROOT_UID);
            setEffectiveGroup(ROOT_GID);
        }
    }
}
Also used : VMRuntime(dalvik.system.VMRuntime) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader)

Example 3 with VMRuntime

use of dalvik.system.VMRuntime in project robovm by robovm.

the class MemoryTest method testSetIntArray.

public void testSetIntArray() {
    int[] values = { 3, 7, 31, 127, 8191, 131071, 524287, 2147483647 };
    int[] swappedValues = new int[values.length];
    for (int i = 0; i < values.length; ++i) {
        swappedValues[i] = Integer.reverseBytes(values[i]);
    }
    int scale = SizeOf.INT;
    VMRuntime runtime = VMRuntime.getRuntime();
    byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, scale * values.length + 1);
    long base_ptr = runtime.addressOf(array);
    for (long ptr_offset = 0; ptr_offset < 2; ++ptr_offset) {
        // To test aligned and unaligned accesses.
        long ptr = base_ptr + ptr_offset;
        Arrays.fill(array, (byte) 0);
        // Regular copy.
        Memory.pokeIntArray(ptr, values, 0, values.length, false);
        assertIntsEqual(values, ptr, false);
        assertIntsEqual(swappedValues, ptr, true);
        // Swapped copy.
        Memory.pokeIntArray(ptr, values, 0, values.length, true);
        assertIntsEqual(values, ptr, true);
        assertIntsEqual(swappedValues, ptr, false);
        // Swapped copies of slices (to ensure we test non-zero offsets).
        for (int i = 0; i < values.length; ++i) {
            Memory.pokeIntArray(ptr + i * scale, values, i, 1, true);
        }
        assertIntsEqual(values, ptr, true);
        assertIntsEqual(swappedValues, ptr, false);
    }
}
Also used : VMRuntime(dalvik.system.VMRuntime)

Example 4 with VMRuntime

use of dalvik.system.VMRuntime in project robovm by robovm.

the class MemoryBlock method allocate.

public static MemoryBlock allocate(int byteCount) {
    VMRuntime runtime = VMRuntime.getRuntime();
    byte[] array = (byte[]) runtime.newNonMovableArray(byte.class, byteCount);
    long address = runtime.addressOf(array);
    return new NonMovableHeapBlock(array, address, byteCount);
}
Also used : VMRuntime(dalvik.system.VMRuntime)

Example 5 with VMRuntime

use of dalvik.system.VMRuntime in project platform_frameworks_base by android.

the class ZygoteInit method preloadResources.

/**
     * Load in commonly used resources, so they can be shared across
     * processes.
     *
     * These tend to be a few Kbytes, but are frequently in the 20-40K
     * range, and occasionally even larger.
     */
private static void preloadResources() {
    final VMRuntime runtime = VMRuntime.getRuntime();
    try {
        mResources = Resources.getSystem();
        mResources.startPreloading();
        if (PRELOAD_RESOURCES) {
            Log.i(TAG, "Preloading resources...");
            long startTime = SystemClock.uptimeMillis();
            TypedArray ar = mResources.obtainTypedArray(com.android.internal.R.array.preloaded_drawables);
            int N = preloadDrawables(ar);
            ar.recycle();
            Log.i(TAG, "...preloaded " + N + " resources in " + (SystemClock.uptimeMillis() - startTime) + "ms.");
            startTime = SystemClock.uptimeMillis();
            ar = mResources.obtainTypedArray(com.android.internal.R.array.preloaded_color_state_lists);
            N = preloadColorStateLists(ar);
            ar.recycle();
            Log.i(TAG, "...preloaded " + N + " resources in " + (SystemClock.uptimeMillis() - startTime) + "ms.");
            if (mResources.getBoolean(com.android.internal.R.bool.config_freeformWindowManagement)) {
                startTime = SystemClock.uptimeMillis();
                ar = mResources.obtainTypedArray(com.android.internal.R.array.preloaded_freeform_multi_window_drawables);
                N = preloadDrawables(ar);
                ar.recycle();
                Log.i(TAG, "...preloaded " + N + " resource in " + (SystemClock.uptimeMillis() - startTime) + "ms.");
            }
        }
        mResources.finishPreloading();
    } catch (RuntimeException e) {
        Log.w(TAG, "Failure preloading resources", e);
    }
}
Also used : VMRuntime(dalvik.system.VMRuntime) TypedArray(android.content.res.TypedArray)

Aggregations

VMRuntime (dalvik.system.VMRuntime)33 TypedArray (android.content.res.TypedArray)7 BufferedReader (java.io.BufferedReader)7 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 InputStreamReader (java.io.InputStreamReader)7 ErrnoException (android.system.ErrnoException)5 FileInputStream (java.io.FileInputStream)5 FileNotFoundException (java.io.FileNotFoundException)5 ByteBuffer (java.nio.ByteBuffer)5 Chunk (org.apache.harmony.dalvik.ddmc.Chunk)5 Properties (java.util.Properties)2 StructUtsname (libcore.io.StructUtsname)2 ErrnoException (libcore.io.ErrnoException)1 StructPasswd (libcore.io.StructPasswd)1