1 | package tuffy.util; |
2 | import java.util.Scanner; |
3 | |
4 | /** |
5 | * Container of methods for debugging purposes. |
6 | */ |
7 | public class DebugMan { |
8 | |
9 | private static StringBuilder log = new StringBuilder(); |
10 | |
11 | public static void log(String s){ |
12 | log.append(s); |
13 | } |
14 | |
15 | public static String getLog(){ |
16 | return log.toString(); |
17 | } |
18 | |
19 | public static void pause() { |
20 | System.out.println("\nPress enter to continue..."); |
21 | Scanner in = new Scanner(System.in); |
22 | in.nextLine(); |
23 | } |
24 | |
25 | public static boolean runningInWindows(){ |
26 | String os = System.getProperty("os.name").toLowerCase(); |
27 | return os.contains("win"); |
28 | } |
29 | |
30 | private static final Runtime s_runtime = Runtime.getRuntime (); |
31 | public static void runGC() throws Exception |
32 | { |
33 | // It helps to call Runtime.gc() |
34 | // using several method calls: |
35 | for (int r = 0; r < 4; ++ r) _runGC (); |
36 | } |
37 | private static void _runGC () throws Exception |
38 | { |
39 | long usedMem1 = usedMemoryp (), usedMem2 = Long.MAX_VALUE; |
40 | for (int i = 0; (usedMem1 < usedMem2) && (i < 500); ++ i) |
41 | { |
42 | s_runtime.runFinalization (); |
43 | s_runtime.gc (); |
44 | Thread.yield(); |
45 | |
46 | usedMem2 = usedMem1; |
47 | usedMem1 = usedMemoryp (); |
48 | } |
49 | } |
50 | |
51 | private static long usedMemoryp () |
52 | { |
53 | return s_runtime.totalMemory () - s_runtime.freeMemory (); |
54 | } |
55 | |
56 | public static long usedMemory(){ |
57 | try{ |
58 | runGC(); |
59 | }catch(Exception e){ |
60 | System.err.println(e.getMessage()); |
61 | } |
62 | long mem0 = Runtime.getRuntime().totalMemory() - |
63 | Runtime.getRuntime().freeMemory(); |
64 | return mem0; |
65 | } |
66 | |
67 | static private long baseMem = 0; |
68 | public static void checkBaseMem(){ |
69 | baseMem = usedMemory(); |
70 | } |
71 | |
72 | public static long getBaseMem(){ |
73 | return baseMem; |
74 | } |
75 | |
76 | |
77 | static private long peakMem = 0; |
78 | public static void checkPeakMem(){ |
79 | long mem = usedMemory(); |
80 | if(mem > peakMem){ |
81 | peakMem = mem; |
82 | } |
83 | } |
84 | |
85 | public static long getPeakMem(){ |
86 | return peakMem; |
87 | } |
88 | |
89 | } |