Example usage for java.lang System gc

List of usage examples for java.lang System gc

Introduction

In this page you can find the example usage for java.lang System gc.

Prototype

public static void gc() 

Source Link

Document

Runs the garbage collector in the Java Virtual Machine.

Usage

From source file:Main.java

public static void recycleBitmapArr(Bitmap[] bmps) {
    for (int i = 0; i < bmps.length; i++) {
        Bitmap mBitmap = bmps[i];/*from   w  ww. j  a va  2 s .c o m*/
        if (mBitmap != null && !mBitmap.isRecycled()) {
            mBitmap.recycle();
            mBitmap = null;
            System.gc();
            System.runFinalization();
            System.gc();
        }
    }
}

From source file:Main.java

public static void cancelAsyncTaskArray(AsyncTask[] dataTasks) {
    if (dataTasks != null) {
        for (int i = 0; i < dataTasks.length; i++) {
            if (dataTasks[i] != null && dataTasks[i].getStatus() != AsyncTask.Status.FINISHED)
                dataTasks[i].cancel(true);
        }/*ww  w . j a  v a  2 s  . c  o  m*/
        dataTasks = null;
        System.gc();
    }
}

From source file:Main.java

static Bitmap createBitmapSafely(int width, int height, Bitmap.Config config, int retryCount) {
    while (retryCount-- > 0) {
        try {/*www. j  a v a  2 s. c  o  m*/
            return Bitmap.createBitmap(width, height, config);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            System.gc();
        }
    }
    return null;
}

From source file:Main.java

public static Bitmap rotateImage(Bitmap bitmap, int rotate) {
    if (rotate != 0) {
        // rotate
        Matrix m = new Matrix();
        m.postRotate(rotate);//from  w  w w .j a v a 2  s.c o  m
        Bitmap rotImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
        bitmap.recycle();
        System.gc();

        return rotImage;
    }
    return bitmap;
}

From source file:Main.java

public static String convertBitmapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    byte[] b = baos.toByteArray();
    String temp = null;/*from  w  w w  . j  a  v  a 2  s. c  om*/
    try {
        System.gc();
        temp = Base64.encodeToString(b, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        b = baos.toByteArray();
        temp = Base64.encodeToString(b, Base64.DEFAULT);
    }
    return temp;
}

From source file:Main.java

private final static void recycle(Bitmap... bitmaps) {
    if (bitmaps == null || bitmaps.length == 0) {
        return;//from w  w  w.  java  2  s.c  o  m
    }
    for (Bitmap bitmap : bitmaps) {
        if (bitmap == null) {
            continue;
        }
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
            continue;
        }
        bitmap = null;
    }
    System.gc();
}

From source file:Main.java

public static Bitmap createBitmap(int width, int height, Config config) {
    for (int i = 0; i < OUT_OF_MEMORY_RETRY_COUNT; ++i) {
        try {//from   w  w  w  .  ja  v a  2 s.co m
            return Bitmap.createBitmap(width, height, config);
        } catch (OutOfMemoryError e) {
            // Retry with GC.
            System.gc();
        }
    }

    return Bitmap.createBitmap(width, height, config);
}

From source file:Main.java

public static void freeeOutputStream(Object stream) {
    if (stream instanceof ByteArrayOutputStream) {
        ByteArrayOutputStream outputFormat = (ByteArrayOutputStream) stream;
        try {//from   w  ww  . j  a  v  a  2s .  c  o  m
            outputFormat.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            outputFormat = null;
        }
    }
    System.gc();
}

From source file:Main.java

protected static Uri scaleDown(Uri imageUri, int targetSize, Context context) {
    System.gc();
    String imagePath = getImagePath(imageUri, context);
    Bitmap currentImage = BitmapFactory.decodeFile(imagePath);

    int targetWidth = targetSize;
    int targetHeight = targetSize;

    int width = currentImage.getWidth();
    int height = currentImage.getHeight();

    if (width < targetWidth || height < targetHeight) {
        currentImage.recycle();//w ww  .j a  v  a2 s  . c o  m
        currentImage = null;
        System.gc();
        return Uri.parse(imageUri.toString());
    }

    height = (int) (height * (float) targetWidth / width);

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(currentImage, targetWidth, height, false);

    if (currentImage != scaledBitmap) {
        currentImage.recycle();
        currentImage = null;
    }
    System.gc();
    File imageFile;
    try {
        imageFile = new File(context.getCacheDir(), "vumatch-upload-00.jpeg");
        FileOutputStream output;

        output = new FileOutputStream(imageFile);
        boolean result = scaledBitmap.compress(CompressFormat.JPEG, 90, output);
        if (result) {
            scaledBitmap.recycle();
            scaledBitmap = null;
            return Uri.fromFile(imageFile);
        } else {
            return null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static <T extends View> T inflateWithOutOfMemoryRetrial(Class<T> clazz, LayoutInflater inflater,
        int resourceId, ViewGroup root, boolean attachToRoot) {
    for (int i = 0; i < OUT_OF_MEMORY_RETRY_COUNT; ++i) {
        try {//from ww  w. j a  va2s  . c  o  m
            return clazz.cast(inflater.inflate(resourceId, root, attachToRoot));
        } catch (OutOfMemoryError e) {
            // Retry with GC.
            System.gc();
        }
    }

    return clazz.cast(inflater.inflate(resourceId, root, attachToRoot));
}