|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 /* |
|
19 * DebugDialog.java |
|
20 */ |
|
21 package com.nokia.carbide.cpp.internal.pi.utils; |
|
22 |
|
23 import java.awt.BorderLayout; |
|
24 import java.awt.GridLayout; |
|
25 |
|
26 import javax.swing.JButton; |
|
27 import javax.swing.JDialog; |
|
28 import javax.swing.JLabel; |
|
29 import javax.swing.JPanel; |
|
30 |
|
31 public class DebugDialog extends JDialog |
|
32 { |
|
33 private static final long serialVersionUID = -7397509550206778612L; |
|
34 private JPanel panel; |
|
35 private JLabel freeMemory; |
|
36 private JLabel totalMemory; |
|
37 private JLabel usedMemory; |
|
38 private static Runtime runtime; |
|
39 private static long time4; |
|
40 |
|
41 private static void setTime4(long newTime) { |
|
42 time4 = newTime; |
|
43 } |
|
44 |
|
45 private static void setRuntime(Runtime newRuntime) |
|
46 { |
|
47 runtime = newRuntime; |
|
48 } |
|
49 |
|
50 public DebugDialog() |
|
51 { |
|
52 setTime4(0); |
|
53 setRuntime(Runtime.getRuntime()); |
|
54 |
|
55 freeMemory = new JLabel(Messages.getString("DebugDialog.notInitialised")); //$NON-NLS-1$ |
|
56 totalMemory = new JLabel(Messages.getString("DebugDialog.notInitialised")); //$NON-NLS-1$ |
|
57 usedMemory = new JLabel(Messages.getString("DebugDialog.notInitialised")); //$NON-NLS-1$ |
|
58 |
|
59 this.setTitle(Messages.getString("DebugDialog.debugDialog")); //$NON-NLS-1$ |
|
60 panel = (JPanel) this.getContentPane(); |
|
61 panel.setLayout(new BorderLayout()); |
|
62 panel.add(getMemoryPanel(), BorderLayout.CENTER); |
|
63 panel.add(getButtonPanel(), BorderLayout.SOUTH); |
|
64 this.pack(); |
|
65 panel.updateUI(); |
|
66 } |
|
67 |
|
68 private JPanel getButtonPanel() |
|
69 { |
|
70 JPanel mp = new JPanel(); |
|
71 mp.add(getRefreshButton()); |
|
72 mp.add(getGarbageCollectionButton()); |
|
73 mp.add(getTestButton()); |
|
74 return mp; |
|
75 } |
|
76 |
|
77 private JPanel getMemoryPanel() |
|
78 { |
|
79 JPanel mp = new JPanel(); |
|
80 mp.setLayout(new GridLayout(3, 2)); |
|
81 |
|
82 mp.add(new JLabel(Messages.getString("DebugDialog.freeMemory"))); //$NON-NLS-1$ |
|
83 mp.add(freeMemory); |
|
84 mp.add(new JLabel(Messages.getString("DebugDialog.totalMemory"))); //$NON-NLS-1$ |
|
85 mp.add(totalMemory); |
|
86 mp.add(new JLabel(Messages.getString("DebugDialog.usedMemory"))); //$NON-NLS-1$ |
|
87 mp.add(usedMemory); |
|
88 // refreshMemoryStatus(); |
|
89 return mp; |
|
90 } |
|
91 |
|
92 public void refreshMemoryStatus() |
|
93 { |
|
94 long free = runtime.freeMemory() / 1000; |
|
95 long total = runtime.totalMemory() / 1000; |
|
96 long used = total - free; |
|
97 |
|
98 freeMemory.setText("" + free); //$NON-NLS-1$ |
|
99 totalMemory.setText("" + total); //$NON-NLS-1$ |
|
100 usedMemory.setText("" + used); //$NON-NLS-1$ |
|
101 } |
|
102 |
|
103 private JButton getRefreshButton() |
|
104 { |
|
105 JButton refresh = new JButton(Messages.getString("DebugDialog.refresh")); //$NON-NLS-1$ |
|
106 refresh.addActionListener(new java.awt.event.ActionListener() |
|
107 { |
|
108 public void actionPerformed(java.awt.event.ActionEvent e) |
|
109 { |
|
110 refreshMemoryStatus(); |
|
111 } |
|
112 }); |
|
113 return refresh; |
|
114 } |
|
115 |
|
116 private JButton getGarbageCollectionButton() |
|
117 { |
|
118 JButton garbage = new JButton(Messages.getString("DebugDialog.garbage")); //$NON-NLS-1$ |
|
119 garbage.addActionListener(new java.awt.event.ActionListener() |
|
120 { |
|
121 public void actionPerformed(java.awt.event.ActionEvent e) |
|
122 { |
|
123 //runs garbage collection |
|
124 garbageCollection(); |
|
125 } |
|
126 }); |
|
127 return garbage; |
|
128 } |
|
129 private JButton getTestButton() |
|
130 { |
|
131 JButton refresh = new JButton(Messages.getString("DebugDialog.test")); //$NON-NLS-1$ |
|
132 refresh.addActionListener(new java.awt.event.ActionListener() |
|
133 { |
|
134 public void actionPerformed(java.awt.event.ActionEvent e) |
|
135 { |
|
136 refreshMemoryStatus(); |
|
137 } |
|
138 }); |
|
139 return refresh; |
|
140 } |
|
141 |
|
142 public static void garbageCollection() |
|
143 { |
|
144 long time1 = System.currentTimeMillis(); |
|
145 //Time time = new Time(System.currentTimeMillis()); |
|
146 runtime.runFinalization(); |
|
147 runtime.gc(); |
|
148 long time2 = System.currentTimeMillis(); |
|
149 long time3 = time2 - time1; |
|
150 time4 = time4 + time3; |
|
151 System.out.println(Messages.getString("DebugDialog.garbageCollected1") + time3 + Messages.getString("DebugDialog.garbageCollected2") + time4 + Messages.getString("DebugDialog.garbageCollected3")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
152 //Time time2 = new Time(System.currentTimeMillis()); |
|
153 } |
|
154 |
|
155 public static void printMemoryUsage() |
|
156 { |
|
157 //Runtime runtime = Runtime.getRuntime(); |
|
158 |
|
159 long free = runtime.freeMemory() / 1000; |
|
160 long total = runtime.totalMemory() / 1000; |
|
161 long used = total - free; |
|
162 System.out.println(Messages.getString("DebugDialog.currentMemory") + used); //$NON-NLS-1$ |
|
163 } |
|
164 |
|
165 public static String getCurrentMemoryUsage() |
|
166 { |
|
167 long free = runtime.freeMemory() / 1000000; |
|
168 long total = runtime.totalMemory() / 1000000; |
|
169 long used = total - free; |
|
170 return "" + used + "/" + total; //$NON-NLS-1$ //$NON-NLS-2$ |
|
171 } |
|
172 |
|
173 } |