MemoryTools

Die Klasse MenoryTools stellt statische Hilfsmethoden Methoden zur Verfügung, welche z.B. Bytes in ein Menschlich lesbares Format konvertiert. Informationen über den Speicherbedarf und andere Laufzeiteigenschaften, kann man sich mit dem Programm VisualVM anzeigen lassen. Dieses liegt dem JDK im Verzeichnis bin bei.


VisualVM

class MemoryTools


package de.snowbits.tools;

import java.text.DecimalFormat;

public class MemoryTools {

	public enum Unit { TB, GB, MB, KB, B; }
	
	private static final String ERROR_MESSAGE = "Error invalid size, bytes is less than zero. bytes: ";

	private static final String[] UNITS = new String[] {
		Unit.TB.toString(), Unit.GB.toString(), Unit.MB.toString(), Unit.KB.toString(), Unit.B.toString()};
	
	private static final long KILO_BYTE = 1024;
	private static final long MEGA_BYTE = KILO_BYTE * KILO_BYTE;
	private static final long GIGA_BYTE = MEGA_BYTE * KILO_BYTE;
	private static final long TERA_BYTE = GIGA_BYTE * KILO_BYTE;
	
	private static final long[] UNIT_SEPARATOR = new long[] {TERA_BYTE, GIGA_BYTE, MEGA_BYTE, KILO_BYTE, 1};
	
	private MemoryTools() { } 

	public static final String convert(long bytes){	    
	    if(bytes < 1)
	        throw new IllegalArgumentException(ERROR_MESSAGE + bytes);

	    String result = null;
	    
	    for(int i = 0; i < UNIT_SEPARATOR.length; i++){
	        if(bytes >= UNIT_SEPARATOR[i]) {
	            double calc = (double) bytes > 1 ? (double) bytes / (double) UNIT_SEPARATOR[i] : (double) UNIT_SEPARATOR[i];
	            result = new DecimalFormat("#,##0.#").format(calc) + " " + UNITS[i];
	            break;
	        }
	    }
	    
	    return result;
	}
		
	public static final String convert(Unit unit, long bytes) {
	    if(bytes < 1)
	        throw new IllegalArgumentException(ERROR_MESSAGE + bytes);
	    
	    switch (unit) {
		case TB:
			return format(unit, (double) (bytes / 1099511627776l));
		case GB:
			return format(unit, (double) (bytes / 1073741824l));
		case MB:
			return format(unit, (double) (bytes / 1048576l));
		case KB:
			return format(unit, (double) (bytes / 1024l));
		default:
			return format(unit, (double) (bytes));
		}
	}	
	
	public static final long calcBytes(Unit unit, long bytes) {
	    if(bytes < 1)
	        throw new IllegalArgumentException(ERROR_MESSAGE + bytes);
	    
	    switch (unit) {
		case TB:
			return bytes * 1099511627776l;
		case GB:
			return bytes * 1073741824l;
		case MB:
			return bytes * 1048576l;
		case KB:
			return bytes * 1024l;
		default:
			return bytes;
		}
	}
	
	public static final boolean gcIsFreeMemoryLessThan(long bytes) {
		if (isFreeMemoryLessThan(bytes)) {
			System.gc();
			return true;
		}
	    
		return false;
	}
	
	public static final boolean isFreeMemoryLessThan(long bytes) {
	    if(bytes < 1)
	        throw new IllegalArgumentException(ERROR_MESSAGE + bytes);
	    
		return Runtime.getRuntime().freeMemory() < bytes;
	}
	
	public static final boolean isFreeMemoryEquals(long bytes) {
	    if(bytes < 1)
	        throw new IllegalArgumentException(ERROR_MESSAGE + bytes);
	    
		return Runtime.getRuntime().freeMemory() == bytes;
	}
	
	public static final Memory getMemory() {
		return new Memory();
	}

	private static final String format(Unit unit, double bytes) {
		return new DecimalFormat("#,##0.#").format(bytes) + " " + unit;
	}
}
	

class Memory


package de.snowbits.tools;

public final class Memory {
	
	private long _free;
	private long _total;
	private long _max;
	
	public Memory() {
		_free = Runtime.getRuntime().freeMemory();
		_total = Runtime.getRuntime().totalMemory();
		_max = Runtime.getRuntime().maxMemory();
	}
	
	public long getFree() {
		return _free;
	}
	public long getTotal() {
		return _total;
	}
	public long getMax() {
		return _max;
	}
	
	public String toStringFormat() {
		return new StringBuilder().append(getClass().getSimpleName()).append("[")
				.append("free: ").append(MemoryTools.convert(getFree()))
				.append("; total: ").append(MemoryTools.convert(getTotal()))
				.append("; max: ").append(MemoryTools.convert(getMax()))
				.append("]").toString();
	}
	
	@Override
	public String toString() {
		return new StringBuilder().append(getClass().getSimpleName()).append("[")
				.append("free: ").append(getFree())
				.append("; total: ").append(getTotal())
				.append("; max: ").append(getMax())
				.append("]").toString();
	}
}
	

class MemoryToolsTest


package de.snowbits.tools;

import de.snowbits.tools.MemoryTools.Unit;

public class MemoryToolsTest {

	public static void main(String[] args) {
	    System.out.println(MemoryTools.convert(1l));
	    System.out.println(MemoryTools.convert(1024));
	    System.out.println(MemoryTools.convert(1048576l));
	    System.out.println(MemoryTools.convert(1073741824l));
	    System.out.println(MemoryTools.convert(1099511627776l));
	    System.out.println("===========================");
	    System.out.println(MemoryTools.convert(Unit.B, 1099511627776l));
	    System.out.println(MemoryTools.convert(Unit.KB, 1099511627776l));
	    System.out.println(MemoryTools.convert(Unit.MB, 1099511627776l));
	    System.out.println(MemoryTools.convert(Unit.GB, 1099511627776l));
	    System.out.println(MemoryTools.convert(Unit.TB, 1099511627776l));
	    System.out.println("===========================");
	    System.out.println(MemoryTools.calcBytes(Unit.B, 1l));
	    System.out.println(MemoryTools.calcBytes(Unit.KB, 1l));
	    System.out.println(MemoryTools.calcBytes(Unit.MB, 1l));
	    System.out.println(MemoryTools.calcBytes(Unit.GB, 1l));
	    System.out.println(MemoryTools.calcBytes(Unit.TB, 1l));
	    System.out.println("===========================");
	    System.out.println(MemoryTools.getMemory().toString());
	    System.out.println("===========================");
	    System.out.println(MemoryTools.getMemory().toStringFormat());
	    System.out.println("===========================");
	    System.out.println(MemoryTools.isFreeMemoryLessThan(64487424));
	    System.out.println("===========================");
	    System.out.println(MemoryTools.isFreeMemoryEquals(64487424));
	    System.out.println("===========================");
	    System.out.println(MemoryTools.gcIsFreeMemoryLessThan(20971520));
	    System.out.println("===========================");
	    System.out.println(MemoryTools.gcIsFreeMemoryLessThan(MemoryTools.calcBytes(Unit.MB, 60l)));
	}

}
	

Ausgabe


1 B
1 KB
1 MB
1 GB
1 TB
===========================
1.099.511.627.776 B
1.073.741.824 KB
1.048.576 MB
1.024 GB
1 TB
===========================
1
1024
1048576
1073741824
1099511627776
===========================
Memory[free: 62136920; total: 64487424; max: 954728448]
===========================
Memory[free: 59,3 MB; total: 61,5 MB; max: 910,5 MB]
===========================
true
===========================
false
===========================
false
===========================
true
	

Top


Example
Sitemap Kontakt Impressum