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 Bitmap createJustBoundsBitmap(Resources res, int resId) {
    Bitmap bm = null;//from   w ww.  jav a2 s .  co  m
    try {
        bm = BitmapFactory.decodeResource(res, resId, getJustBoundsOptions());
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return bm;
}

From source file:Main.java

public static Bitmap createBitmap(String filePath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 4;//  w ww  . j ava2 s.c  o  m
    Bitmap b = null;
    try {
        b = BitmapFactory.decodeFile(filePath, options);
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return b;
}

From source file:Main.java

public static boolean saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
    boolean saveComplete = true;
    try {/*  www.  j  av a2  s  . c o m*/
        f.createNewFile();
        FileOutputStream fOut = null;
        fOut = new FileOutputStream(f);
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        int finalWidth = 800;
        int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0));
        double x = width * finalHeight;
        double y = height * finalWidth;

        if (x > y) {
            finalHeight = (int) (y / (double) width);
        } else if (x < y) {
            finalWidth = (int) (x / (double) height);
        }

        if (finalWidth > width && finalHeight > height) {
            finalWidth = width;
            finalHeight = height;
        }
        Matrix matrix = new Matrix();
        matrix.reset();
        float scaleWidth = ((float) finalWidth) / (float) width;
        float scaleHeight = ((float) finalHeight) / (float) height;
        matrix.postScale(scaleWidth, scaleHeight);
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width, (int) height, matrix, true);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
        fOut.flush();
        fOut.close();
        mBitmap.recycle();
        System.gc();
    } catch (FileNotFoundException e) {
        saveComplete = false;
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        saveComplete = false;
    }
    return saveComplete;
}

From source file:Main.java

public static Bitmap createOriginalBitmap(String filePath) {
    Bitmap b = null;/*from  w  w w  .  j  ava 2  s . c o m*/
    try {
        b = BitmapFactory.decodeFile(filePath, null);
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return b;
}

From source file:CollectionTest.java

static void cleanGC() {
    System.gc();
    System.runFinalization();
    System.gc();
}

From source file:Main.java

public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) {
    try {/*from   ww w  . ja  v a 2 s .  c  o  m*/
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        int index = tempFileName.lastIndexOf(".");
        if (index != -1)
            tempFileName = tempFileName.substring(0, index);
        File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();
        Config type = srcBitmap.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height);
        srcBitmap.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        srcBitmap.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        srcBitmap = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        srcBitmap.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temp file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return srcBitmap;
}

From source file:com.google.cloud.bigtable.hbase.PutAdapterPerf.java

private static void putAdapterPerf(HBaseRequestAdapter adapter, Put put) {
    System.out.println("Size: " + adapter.adapt(put).getSerializedSize());
    System.gc();
    {/*  www .jav a 2 s .c  o m*/
        long start = System.nanoTime();
        for (int i = 0; i < count; i++) {
            adapter.adapt(put);
        }
        long time = System.nanoTime() - start;
        System.out.println(String.format("adapted: %,d rows merged in %,d ms.  %,d nanos per row.", count,
                time / 1000000, time / count));
    }
    System.gc();
    {
        long start = System.nanoTime();
        for (int i = 0; i < count; i++) {
            MutateRowRequest adapted = adapter.adapt(put);
            BigtableGrpc.METHOD_MUTATE_ROW.streamRequest(adapted);
        }
        long time = System.nanoTime() - start;
        System.out
                .println(String.format("adapted, streamRequest: %,d rows merged in %,d ms.  %,d nanos per row.",
                        count, time / 1000000, time / count));
    }
    System.gc();
    {
        long start = System.nanoTime();
        for (int i = 0; i < count; i++) {
            MutateRowRequest adapted = adapter.adapt(put);
            adapted.getSerializedSize();
            BigtableGrpc.METHOD_MUTATE_ROW.streamRequest(adapted);
        }
        long time = System.nanoTime() - start;
        System.out.println(String.format(
                "adapted, getSerializedSize, streamRequest: %,d rows merged in %,d ms.  %,d nanos per row.",
                count, time / 1000000, time / count));
    }
}

From source file:com.imagelake.control.CreditsDAOImp.java

@Override
public Credits getCreditDetails(int credit_id) {
    Credits c = new Credits();
    try {/*from  w  w w.  j  a va  2s.c om*/
        String sql = "SELECT * FROM credits WHERE credit_id=?";
        Connection con = DBFactory.getConnection();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setInt(1, credit_id);
        ResultSet rs = ps.executeQuery();
        System.gc();
        while (rs.next()) {
            c = new Credits();
            c.setCredit_id(rs.getInt(1));
            c.setCredits(rs.getInt(2));
            c.setSize(rs.getString(3));
            c.setWidth(rs.getInt(4));
            c.setHeight(rs.getInt(5));
            c.setState(rs.getInt(6));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return c;
}

From source file:Main.java

public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) {
    if (null != bitmapUrl) {
        InputStream inputStream = null;
        try {/*  w  ww  . j a va2s .c  o  m*/
            URL url = new URL(bitmapUrl);

            URLConnection connection = url.openConnection();

            connection.connect();

            inputStream = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int read;

            while ((read = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, read);
            }
            inputStream.close();
            byteArrayOutputStream.close();

            buffer = byteArrayOutputStream.toByteArray();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);

            int maxSize = Math.max(options.outWidth, options.outHeight);
            float newImageScale = 1f;
            if (-1 != imageSize) {
                newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density);
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.round(newImageScale);

            return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        } catch (Throwable e) {
            // pass
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // pass
                }
                inputStream = null;
            }
            System.gc();
        }
    }

    return null;
}

From source file:com.sec.ose.osi.sdk.protexsdk.discovery.ReportAPIWrapper.java

protected static void discardAllData() {
    mReportTable.clear();
    System.gc();
}