KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > room


1 /*
2  * room.java
3  *
4  * Copyright (C) 2003-2004 Peter Graves
5  * $Id: room.java,v 1.7 2004/01/28 20:19:22 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 // ### room
25
public final class room extends Primitive
26 {
27     private room()
28     {
29         super("room", "&optional x");
30     }
31
32     public LispObject execute(LispObject[] args) throws ConditionThrowable
33     {
34         Runtime JavaDoc runtime = Runtime.getRuntime();
35         long total = 0;
36         long free = 0;
37         long maxFree = 0;
38         while (true) {
39             try {
40                 runtime.gc();
41                 Thread.currentThread().sleep(100);
42                 runtime.runFinalization();
43                 Thread.currentThread().sleep(100);
44                 runtime.gc();
45                 Thread.currentThread().sleep(100);
46             }
47             catch (InterruptedException JavaDoc e) {}
48             total = runtime.totalMemory();
49             free = runtime.freeMemory();
50             if (free > maxFree)
51                 maxFree = free;
52             else
53                 break;
54         }
55         long used = total - free;
56         Stream out = getStandardOutput();
57         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Total memory ");
58         sb.append(total);
59         sb.append(" bytes");
60         sb.append(System.getProperty("line.separator"));
61         sb.append(used);
62         sb.append(" bytes used");
63         sb.append(System.getProperty("line.separator"));
64         sb.append(free);
65         sb.append(" bytes free");
66         sb.append(System.getProperty("line.separator"));
67         out._writeString(sb.toString());
68         out._finishOutput();
69         return number(used);
70     }
71
72     private static final room ROOM = new room();
73 }
74
Popular Tags