001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU General Public License Version 2 only ("GPL") or the Common Development and Distribution License("CDDL") (collectively, the "License"). You may not use this file except in compliance with the
007: * License. You can obtain a copy of the License at http://www.netbeans.org/cddl-gplv2.html or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the specific language governing permissions and limitations under the
008: * License. When distributing the software, include this License Header Notice in each file and include the License file at nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this particular file as subject to the "Classpath" exception as provided
009: * by Sun in the GPL Version 2 section of the License file that accompanied this code. If applicable, add the following below the License Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]"
010: *
011: * Contributor(s):
012: *
013: * The Original Software is NetBeans. The Initial Developer of the Original Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
014: *
015: * If you wish your version of this file to be governed by only the CDDL or only the GPL Version 2, indicate your decision by adding "[Contributor] elects to include this software in this distribution under the [CDDL or GPL Version 2] license." If you do not indicate a
016: * single choice of license, a recipient has the option to distribute your version of this file under either the CDDL, the GPL Version 2 or to extend the choice of license to its licensees as provided above. However, if you add GPL Version 2 code and therefore, elected the GPL
017: * Version 2 license, then the option applies only if the new code is made subject to such option by the copyright holder.
018: */
019:
020: package examples.advanced;
021:
022: import java.text.MessageFormat;
023: import java.util.*;
024:
025: import javax.swing.Timer;
026:
027: /** Frame to display amount of free memory in the running application.
028: * <P>
029: * Handy for use with the IDE's internal execution. Then the statistic
030: * of free memory in the whole environment is displayed.
031: */
032: public class MemoryView extends Helper {
033: /** bundle to use */
034: private static ResourceBundle bundle;
035: /** message of free memory */
036: private static MessageFormat msgMemory;
037:
038: /** default update time */
039: private static int UPDATE_TIME = 1000;
040: /** timer to invoke updating */
041: private Timer timer;
042:
043: {
044: bundle = ResourceBundle
045: .getBundle("examples.advanced.MemoryViewLocale");
046: msgMemory = new MessageFormat(bundle.getString("TXT_STATUS"));
047: timer = null;
048: }
049:
050: /** Initializes the Form */
051: public MemoryView() {
052: Class clazz = java.lang.Runtime.class;
053: String string = "Hi!";
054: int n = 50;
055: List llist = new LinkedList();
056: List alist = new ArrayList();
057: List vec = new Vector();
058: Map hmap = new HashMap();
059: Map htab = new Hashtable();
060: Map tmap = new TreeMap();
061: Set hset = new HashSet();
062: Set tset = new TreeSet();
063: for (int i = 0; i < n; i++) {
064: String s = i + ". item";
065: llist.add(s);
066: alist.add(s);
067: vec.add(s);
068: hset.add(s);
069: tset.add(s);
070: hmap.put("" + i, s);
071: htab.put("" + i, s);
072: tmap.put("" + i, s);
073: }
074: int[] policko = new int[] { 1, 2, 3, 4, 5 };
075: int[] pole = new int[n];
076: int[][] d2 = new int[10][20];
077: }
078:
079: public void updateConsumption() {
080: for (int i = 0; i < 180; i++) {
081: updateStatus();
082: try {
083: Thread.sleep(UPDATE_TIME++);
084: } catch (java.lang.InterruptedException ex) {
085: ex.printStackTrace();
086: }
087: }
088: }
089:
090: /** Updates the status of memory consumption */
091: private int updateStatus() {
092: Runtime r = Runtime.getRuntime();
093: long free = r.freeMemory();
094: long total = r.totalMemory();
095:
096: // when bigger than integer then divide by two
097: while (total > Integer.MAX_VALUE) {
098: total = total >> 1;
099: free = free >> 1;
100: }
101:
102: int taken = (int) (total - free);
103:
104: System.out
105: .println((msgMemory.format(new Object[] {
106: new Long(total), new Long(free),
107: new Integer(taken) })));
108: return taken;
109: }
110:
111: public static void main(java.lang.String[] args) {
112: MemoryView mv = new MemoryView();
113: mv.inner();
114: mv.test();
115: mv.updateConsumption();
116: }
117:
118: public void inner() {
119: Thread t = new Thread(new Runnable() {
120: public void run() {
121: System.out.println("I\'m in anonymous inner class.");
122: }
123: });
124: t.start();
125: try {
126: t.join();
127: } catch (InterruptedException e) {
128: }
129: }
130:
131: public String Vpublic = "Public Variable";
132: protected String Vprotected = "Protected Variable";
133: private String Vprivate = "Private Variable";
134: String VpackagePrivate = "Package-private Variable";
135: public static String Spublic = "Public Variable";
136: protected static String Sprotected = "Protected Variable";
137: private static String Sprivate = "Private Variable";
138: static String SpackagePrivate = "Package-private Variable";
139: }
140:
141: class Helper {
142: public String inheritedVpublic = "Inherited Public Variable";
143: protected String inheritedVprotected = "Inherited Protected Variable";
144: private String inheritedVprivate = "Inherited Private Variable";
145: String inheritedVpackagePrivate = "Inherited Package-private Variable";
146: public static String inheritedSpublic = "Inherited Public Variable";
147: protected static String inheritedSprotected = "Inherited Protected Variable";
148: private static String inheritedSprivate = "Inherited Private Variable";
149: static String inheritedSpackagePrivate = "Inherited Package-private Variable";
150:
151: public void test() {
152: System.out.println("I\'m in secondary class");
153: }
154: }
|