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

/**
 * Creates a bitmap with the given width and height.
 * <p/>/*w  w  w  . j a  v  a  2  s  .co  m*/
 * If it fails with an OutOfMemory error, it will force a GC and then try to create the bitmap
 * one more time.
 *
 * @param width  width of the bitmap
 * @param height height of the bitmap
 */
public static Bitmap createBitmapAndGcIfNecessary(int width, int height) {
    try {
        return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError e) {
        System.gc();
        return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
    }
}

From source file:Main.java

public static Bitmap loadImage(int bitmapId, Resources resources, int tries) {
    Bitmap bitmap;//www .j  a  v a  2 s .  co m

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        bitmap = BitmapFactory.decodeResource(resources, bitmapId, options);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();

        System.gc();

        if (--tries > 0) {
            return loadImage(bitmapId, resources, tries);
        } else {
            return null;
        }
    }

    return bitmap;
}

From source file:Main.java

public static void memoryPanic() {
    try {/*from   w  ww . j a va2s . co  m*/
        System.gc();
        Thread.sleep(250);
    } catch (Exception ex) {
    }
}

From source file:Main.java

private static Bitmap getImage(String imagePath) {
    final int MAX_TRIALS = 3;
    int trial = 0;
    Bitmap bitmap = null;/*www. j ava2s .  c o m*/
    do {
        try {
            bitmap = BitmapFactory.decodeFile(imagePath);
        } catch (OutOfMemoryError e) {
            System.gc();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                throw new RuntimeException("OoM");
            }
        }
    } while (trial++ != MAX_TRIALS);
    if (trial == MAX_TRIALS || bitmap == null) {
        throw new RuntimeException("OoM");
    }
    return bitmap;
}

From source file:Main.java

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

    return Bitmap.createBitmap(src);
}

From source file:Main.java

public static Bitmap allocateBitmap(int width, int height) {
    Bitmap bitmap = null;// w  w w.j a  va 2  s  .  co  m
    try {
        bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    } catch (Throwable e1) {
        System.gc();
        try {
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        } catch (Throwable e2) {
            bitmap = null;
        }
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap readBitmapFromPath(Activity context, String filePath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   www .  j  a v a2  s.c  o  m*/
    BitmapFactory.decodeFile(filePath, options);
    int outWidth = options.outWidth;
    int outHeight = options.outHeight;
    options.inJustDecodeBounds = false;
    int be = calculateInSampleSize(context, outWidth, outHeight);
    options.inSampleSize = be;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    try {
        return BitmapFactory.decodeFile(filePath, options);
    } catch (OutOfMemoryError e) {
        System.gc();
        try {
            options.inSampleSize = be + 1;
            return BitmapFactory.decodeFile(filePath, options);

        } catch (OutOfMemoryError e2) {
            return null;
        }
    }
}

From source file:Main.java

public static Bitmap getImageBitmapFromAssetsFolderThroughImagePathName(Context context, String imagePathName,
        int reqWidth, int reqHeight) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;/*from w w w.  j av  a2 s  . c  om*/
    Bitmap bitmap = null;
    AssetManager assetManager = context.getResources().getAssets();
    InputStream inputStream = null;

    try {
        inputStream = assetManager.open(imagePathName);
        inputStream.mark(Integer.MAX_VALUE);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    }

    try {
        if (inputStream != null) {
            BitmapFactory.decodeStream(inputStream, null, opts);
            inputStream.reset();
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    opts.inSampleSize = calculateInSampleSiez(opts, reqWidth, reqHeight);
    //         Log.d(TAG,""+opts.inSampleSize);
    opts.inJustDecodeBounds = false;
    opts.inPreferredConfig = Bitmap.Config.RGB_565;
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    opts.inDither = false;
    opts.inTempStorage = new byte[512 * 1024];
    try {
        if (inputStream != null) {
            bitmap = BitmapFactory.decodeStream(inputStream, null, opts);
        } else {
            return null;
        }
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        System.gc();
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    //Log.d(TAG,"w:"+bitmap.getWidth()+" h:"+bitmap.getHeight());
    if (bitmap != null) {
        try {
            bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
        } catch (OutOfMemoryError outOfMemoryError) {
            outOfMemoryError.printStackTrace();
            System.gc();
            return null;
        }
    }
    return bitmap;
}

From source file:GCTask.java

public void run() {
    System.out.println("Running the scheduled task...");
    System.gc();
}

From source file:Main.java

public static String saveImg(Bitmap b, String name) throws Exception {
    String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "QuCai/shareImg/";
    File mediaFile = new File(path + File.separator + name + ".jpg");
    if (mediaFile.exists()) {
        mediaFile.delete();//from  w w w  .java 2 s  . c o  m

    }
    if (!new File(path).exists()) {
        new File(path).mkdirs();
    }
    mediaFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(mediaFile);
    b.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
    b.recycle();
    b = null;
    System.gc();
    return mediaFile.getPath();
}