01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: */
19: package de.schlund.pfixcore.util;
20:
21: import org.apache.log4j.Logger;
22:
23: public final class Meminfo {
24:
25: //~ Instance/static variables ..................................................................
26:
27: private static long last_used = -1L;
28: private final static Logger LOG = Logger.getLogger(Meminfo.class);
29:
30: //~ Methods ....................................................................................
31:
32: public final static synchronized void print(String info) {
33: if (!LOG.isDebugEnabled()) {
34: return;
35: }
36: long free;
37: long total;
38: long used_new_bg;
39: long used_new_ag;
40: Runtime run = Runtime.getRuntime();
41: free = run.freeMemory();
42: total = run.totalMemory();
43: used_new_bg = total - free;
44: StringBuffer debugString = new StringBuffer(512);
45: debugString
46: .append("\n,---------------------------------------------------------------\n");
47: if (info != null) {
48: debugString.append("|").append(info).append("\n");
49: }
50: debugString.append("| Meminfo (before GC): ").append(free)
51: .append(" free, ").append(total).append(" total => ")
52: .append(used_new_bg).append(" used.").append("\n");
53: run.gc();
54: free = run.freeMemory();
55: total = run.totalMemory();
56: used_new_ag = total - free;
57: debugString.append("| Meminfo (after GC): ").append(free)
58: .append(" free, ").append(total).append(" total => ")
59: .append(used_new_ag).append(" used.").append("\n");
60: long freed = (used_new_bg - used_new_ag);
61: if (freed > 0) {
62: debugString.append("| => ").append(freed).append(
63: " freed by GC.").append("\n");
64: } else if (freed < 0) {
65: debugString.append("| ???? => GC did COST ")
66: .append(-freed).append(" ????").append("\n");
67: }
68: if (last_used != -1) {
69: debugString.append("| => ").append(
70: used_new_ag - last_used).append(
71: " difference to last run.").append("\n");
72: }
73: debugString
74: .append("`---------------------------------------------------------------\n");
75: last_used = used_new_ag;
76: LOG.debug(debugString.toString());
77: }
78: }
|