001    /**
002     * Copyright (c) 2010, SIB. All rights reserved.
003     * 
004     * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005     * https://sourceforge.net/projects/javaprotlib/
006     * 
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions are met:
009     * Redistributions of source code must retain the above copyright notice, this
010     * list of conditions and the following disclaimer. Redistributions in binary
011     * form must reproduce the above copyright notice, this list of conditions and
012     * the following disclaimer in the documentation and/or other materials provided
013     * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014     * its contributors may be used to endorse or promote products derived from this
015     * software without specific prior written permission.
016     * 
017     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020     * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     */
028    package org.expasy.jpl.commons.base.mem;
029    
030    
031    /**
032     * This class evaluates the amount of memory used, free and total.
033     * 
034     * @author nikitin
035     * 
036     * @version 1.0
037     * 
038     */
039    public final class RunTimeMemoryEstimator {
040            
041            private static long fSLEEP_INTERVAL = 500;
042            
043            private static Runtime RUNTIME = Runtime.getRuntime();
044            
045            private static final long maxMemory = RUNTIME.maxMemory();
046            private static long allocatedMemory;
047            private static long freeAllocatedMemory;
048            
049            public static final long getUsedMemory() {
050                    collectGarbage();
051                    allocatedMemory = Runtime.getRuntime().totalMemory();
052                    
053                    collectGarbage();
054                    freeAllocatedMemory = Runtime.getRuntime().freeMemory();
055                    
056                    return allocatedMemory - freeAllocatedMemory;
057            }
058            
059            public static final long traceMemory() {
060                    getUsedMemory();
061                    
062                    System.out.println("allocated memory: " + allocatedMemory / 1024
063                        + " KB");
064                    System.out.println("allocated free memory: " + freeAllocatedMemory
065                        / 1024 + " KB");
066                    System.out.println("max memory: " + maxMemory / 1024 + " KB");
067                    System.out.println("total free memory: " + getTotalFreeMemory() / 1024
068                        + " KB");
069                    return 0;
070            }
071            
072            public static final long getTotalFreeMemory() {
073                    return freeAllocatedMemory + maxMemory - allocatedMemory;
074            }
075            
076            private static void collectGarbage() {
077                    try {
078                            System.gc();
079                            Thread.sleep(fSLEEP_INTERVAL);
080                            System.runFinalization();
081                            Thread.sleep(fSLEEP_INTERVAL);
082                    } catch (InterruptedException ex) {
083                            ex.printStackTrace();
084                    }
085            }
086            
087    }