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 byte[] getBytesFromFile(File f) {

    if (f == null) {
        return null;
    }/*w ww .  j av a2s  . c o m*/

    FileInputStream stream = null;
    ByteArrayOutputStream out = null;

    try {
        stream = new FileInputStream(f);
        out = new ByteArrayOutputStream(1024);
        byte[] b = new byte[1024];
        int n;
        while ((n = stream.read(b)) != -1)
            out.write(b, 0, n);

    } catch (Exception e) {
    }

    try {
        if (out != null)
            out.close();

        if (stream != null)
            stream.close();
    } catch (Exception e) {
    }

    System.gc();

    if (out == null)
        return null;

    return out.toByteArray();
}

From source file:it.acubelab.smaph.learn.TuneModel.java

public static void main(String[] args) throws Exception {
    Locale.setDefault(Locale.US);
    String freebKey = "<FREEBASE_KEY>";
    String bingKey = "<BING_KEY>";

    WikipediaApiInterface wikiApi = new WikipediaApiInterface("benchmark/cache/wid.cache",
            "benchmark/cache/redirect.cache");
    FreebaseApi freebApi = new FreebaseApi(freebKey, "freeb.cache");

    Vector<ModelConfigurationResult> bestEQFModels = new Vector<>();
    Vector<ModelConfigurationResult> bestEFModels = new Vector<>();
    int wikiSearchTopK = 5; // <======== mind this
    double gamma = 1.0;
    double C = 1.0;
    for (double editDistanceThr = 0.7; editDistanceThr <= 0.7; editDistanceThr += 0.1) {
        WikipediaToFreebase wikiToFreebase = new WikipediaToFreebase("mapdb");

        SmaphAnnotator bingAnnotator = GenerateTrainingAndTest.getDefaultBingAnnotator(wikiApi, wikiToFreebase,
                editDistanceThr, wikiSearchTopK, bingKey);
        SmaphAnnotator.setCache("bing.cache.full");

        BinaryExampleGatherer trainEntityFilterGatherer = new BinaryExampleGatherer();
        BinaryExampleGatherer develEntityFilterGatherer = new BinaryExampleGatherer();
        GenerateTrainingAndTest.gatherExamplesTrainingAndDevel(bingAnnotator, trainEntityFilterGatherer,
                develEntityFilterGatherer, wikiApi, wikiToFreebase, freebApi);

        SmaphAnnotator.unSetCache();/*from  w  w  w .  j  av  a 2s.  c  o  m*/

        Pair<Vector<ModelConfigurationResult>, ModelConfigurationResult> modelAndStatsEF = trainIterative(
                trainEntityFilterGatherer, develEntityFilterGatherer, editDistanceThr,
                OptimizaionProfiles.MAXIMIZE_MACRO_F1, -1.0, gamma, C);
        /*
         * Pair<Vector<ModelConfigurationResult>, ModelConfigurationResult>
         * modelAndStatsEF = trainIterative( trainEmptyQueryGatherer,
         * develEmptyQueryGatherer, editDistanceThr,
         * OptimizaionProfiles.MAXIMIZE_TN, 0.02);
         */
        /*
         * for (ModelConfigurationResult res : modelAndStatsEQF.first)
         * System.out.println(res.getReadable());
         */
        for (ModelConfigurationResult res : modelAndStatsEF.first)
            System.out.println(res.getReadable());

        /* bestEQFModels.add(modelAndStatsEQF.second); */
        bestEFModels.add(modelAndStatsEF.second);
        System.gc();
    }

    for (ModelConfigurationResult modelAndStatsEQF : bestEQFModels)
        System.out.println("Best EQF:" + modelAndStatsEQF.getReadable());
    for (ModelConfigurationResult modelAndStatsEF : bestEFModels)
        System.out.println("Best EF:" + modelAndStatsEF.getReadable());

    System.out.println("Flushing Bing API...");

    SmaphAnnotator.flush();
    wikiApi.flush();
}

From source file:Main.java

private static void triggerGC() throws InterruptedException {
    System.out.println("\n-- Starting GC");
    System.gc();
    Thread.sleep(100);/*from  w  w  w.  j a va  2  s. c  o m*/
    System.out.println("-- End of GC\n");
}

From source file:Main.java

public static Bitmap rotateAndMirror(Bitmap bitmap, int degree, boolean isMirror) throws OutOfMemoryError {

    if ((degree != 0 || isMirror) && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degree, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);

        if (isMirror) {
            m.postScale(-1, 1);//  w  ww .  j  a v a 2 s.  co  m
            degree = (degree + 360) % 360;
            if (degree == 0 || degree == 180) {
                m.postTranslate((float) bitmap.getWidth(), 0);
            } else if (degree == 90 || degree == 270) {
                m.postTranslate((float) bitmap.getHeight(), 0);
            } else {
                throw new IllegalArgumentException("Invalid degrees=" + degree);
            }
        }

        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
        if (bitmap != bitmap2) {
            bitmap.recycle();
            System.gc();
            bitmap = bitmap2;
        }
    }
    return bitmap;
}

From source file:Main.java

public static File getFileFromBytes(byte[] b, String outputFile) {
    BufferedOutputStream stream = null;
    File file = null;//from w  w  w . jav a  2 s.c  om
    FileOutputStream fstream = null;
    try {
        file = new File(outputFile);
        if (!file.exists()) {
            file.createNewFile();
        }
        fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (stream != null) {
        try {
            stream.close();
            fstream.close();

        } catch (Exception e1) {
            e1.printStackTrace();
        }

    }

    System.gc();

    return file;
}

From source file:Main.java

public static Bitmap createPOT(Bitmap bmp) {
    Bitmap potBmp = null;//  w w  w  .  j  a  v  a 2  s .c  o  m
    int potWidth = (int) Math.ceil(Math.log(bmp.getWidth()) / Math.log(2));
    int potHeight = (int) Math.ceil(Math.log(bmp.getHeight()) / Math.log(2));
    potHeight = (int) Math.pow(2, potHeight);
    potWidth = (int) Math.pow(2, potWidth);

    if (potWidth == 1) {
        potWidth = 2;
    }
    if (potHeight == 1) {
        potHeight = 2;
    }

    if (potHeight != bmp.getHeight() || potWidth != bmp.getWidth()) {
        int[] colors = new int[potWidth * potHeight];
        int index = 0;

        int offset = potHeight - bmp.getHeight();
        for (int i = 0; i < potHeight; i++) {
            for (int j = 0; j < potWidth; j++) {
                if (i > offset - 1) {
                    if (j < bmp.getWidth() && i < bmp.getHeight()) {
                        colors[index] = bmp.getPixel(j, i);
                    } else {
                        colors[index] = 0;
                    }
                }
                index++;
            }
        }

        potBmp = Bitmap.createBitmap(colors, potWidth, potHeight, bmp.getConfig());
    } else {
        potBmp = bmp;
    }

    System.gc();

    return potBmp;
}

From source file:Main.java

/**
 * Shrinks and rotates (if necessary) a passed Bitmap.
 *
 * @param bm//from w w w. j a  v  a  2 s .  c  om
 * @param maxLengthOfEdge
 * @param rotateXDegree
 * @return Bitmap
 */
public static Bitmap shrinkBitmap(Bitmap bm, int maxLengthOfEdge, int rotateXDegree) {
    if (maxLengthOfEdge > bm.getWidth() && maxLengthOfEdge > bm.getHeight()) {
        return bm;
    } else {
        // shrink image
        float scale = (float) 1.0;
        if (bm.getHeight() > bm.getWidth()) {
            scale = ((float) maxLengthOfEdge) / bm.getHeight();
        } else {
            scale = ((float) maxLengthOfEdge) / bm.getWidth();
        }
        // CREATE CommonAsync MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scale, scale);
        matrix.postRotate(rotateXDegree);

        // RECREATE THE NEW BITMAP
        bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, false);

        matrix = null;
        System.gc();

        return bm;
    }
}

From source file:com.cloudera.oryx.ml.serving.als.model.LoadTestALSModelFactory.java

public static ALSServingModel buildTestModel() {

    log.info("Building load test model...");

    System.gc();
    long startMemory = JVMUtils.getUsedMemory();

    RandomGenerator random = RandomManager.getRandom();
    PoissonDistribution itemPerUserDist = new PoissonDistribution(random, AVG_ITEMS_PER_USER,
            PoissonDistribution.DEFAULT_EPSILON, PoissonDistribution.DEFAULT_MAX_ITERATIONS);
    ALSServingModel model = new ALSServingModel(FEATURES, true);

    long totalEntries = 0;
    for (int user = 0; user < USERS; user++) {
        String userID = "U" + user;
        model.setUserVector(userID, randomVector(random));
        int itemsPerUser = itemPerUserDist.sample();
        totalEntries += itemsPerUser;//w  w w.j a  v  a 2  s.c o  m
        Collection<String> knownIDs = new ArrayList<>(itemsPerUser);
        for (int i = 0; i < itemsPerUser; i++) {
            knownIDs.add("I" + random.nextInt(ITEMS));
        }
        model.addKnownItems(userID, knownIDs);
    }

    for (int item = 0; item < ITEMS; item++) {
        model.setItemVector("I" + item, randomVector(random));
    }

    System.gc();
    long endMemory = JVMUtils.getUsedMemory();

    log.info("Built model over {} users, {} items, {} features, {} entries, using {}MB", USERS, ITEMS, FEATURES,
            totalEntries, (endMemory - startMemory) / 1_000_000);
    return model;
}

From source file:Main.java

public static Bitmap createBitmapSafely(int width, int height, Bitmap.Config config, int retryCount) {
    try {/*www  .j ava2s .c om*/
        return Bitmap.createBitmap(width, height, config);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        if (retryCount > 0) {
            System.gc();
            return createBitmapSafely(width, height, config, retryCount - 1);
        }
        return null;
    }
}

From source file:Main.java

public static Bitmap createOriginalBitmap(InputStream is) {
    Bitmap bm = null;/*from w ww  .j a va  2 s .c o  m*/
    try {
        bm = BitmapFactory.decodeStream(is, null, null);
    } catch (OutOfMemoryError e) {
        System.gc();
    }
    return bm;
}