KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > util > Mem


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.util;
32
33 public abstract class Mem {
34
35     public static long free () {
36         return Runtime.getRuntime ().freeMemory ();
37     }
38
39     public static long used () {
40         Runtime JavaDoc r = Runtime.getRuntime ();
41         return r.totalMemory() - r.freeMemory ();
42     }
43
44     public static long total () {
45         return Runtime.getRuntime ().totalMemory ();
46     }
47
48     public static String JavaDoc getReport () {
49         return "Memory: used " + (used()/1000) + "KB, free "
50             + (free()/1000) + "KB, total " + (total()/1000) + "KB";
51     }
52
53     public static void gc () {
54         Runtime JavaDoc r = Runtime.getRuntime ();
55         r.runFinalization ();
56         r.gc ();
57     }
58
59     public static void dumpThreadInfo () {
60         ThreadGroup JavaDoc g = Thread.currentThread().getThreadGroup ();
61         Thread JavaDoc[] t = new Thread JavaDoc[g.activeCount ()];
62         g.enumerate (t);
63         System.err.println ("Active threads in " + g);
64         for (int i=0; i<t.length; ++i)
65             System.err.println (t[i]);
66     }
67
68 }
69
Popular Tags