Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/flexstor/common/util/RuntimeUtils.java


1   /*
2    * RuntimeUtils.java
3    *
4    * Copyright $Date: 2003/08/11 02:22:30 $ FLEXSTOR.net Inc.
5    *
6    * This work is licensed for use and distribution under license terms found at
7    * http://www.flexstor.org/license.html
8    *
9    */
10  
11  package com.flexstor.common.util;
12  
13  public class RuntimeUtils
14  {
15     /** The smallest memory amount considered safe to keep FLEXSTOR.db running. */
16     public static final long LOW_MEMORY_LIMIT = 512000; // 500 Kb
17     
18     /**
19      * Check if current available free memory is under the lower allowed limit.
20      * This method will try to invoque garbage collection as an attempt for increasing
21      * the available free memory.
22      *
23      * This method is intended to help prevent situations that could lead into an
24      * OutOfMemoryError. It should be called before executing known memory
25      * consuming operations.
26      *
27      * @return true if available free memory is under the lower allowed limit.
28      */
29     public static boolean isMemoryLow()
30     {
31        if ( Runtime.getRuntime().freeMemory() < LOW_MEMORY_LIMIT )
32        {
33           Runtime.getRuntime().gc();
34  
35           return ( Runtime.getRuntime().freeMemory() < LOW_MEMORY_LIMIT );
36        }
37        
38        return false;
39     }
40  
41     /**
42      * Invokes Garbage Collector.
43      */
44     public static void cleanUpMemory()
45     {
46        Runtime.getRuntime().gc();
47     }
48  
49  } // end of class
50