1 |
package skrueger.swing; |
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Graphics; |
5 |
import java.awt.event.MouseAdapter; |
6 |
import java.awt.event.MouseEvent; |
7 |
import java.util.Locale; |
8 |
import java.util.Timer; |
9 |
import java.util.TimerTask; |
10 |
|
11 |
import javax.swing.JProgressBar; |
12 |
import javax.swing.SwingUtilities; |
13 |
|
14 |
import org.apache.log4j.Logger; |
15 |
|
16 |
import schmitzm.geotools.gui.GeotoolsGUIUtil; |
17 |
import schmitzm.lang.LangUtil; |
18 |
import schmitzm.lang.ResourceProvider; |
19 |
import schmitzm.swing.SwingUtil; |
20 |
import skrueger.swing.formatter.MbDecimalFormatter; |
21 |
|
22 |
public class HeapBar extends JProgressBar { |
23 |
private static final Logger LOGGER = Logger.getLogger(HeapBar.class); |
24 |
|
25 |
protected static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
26 |
.extendPackagePath(SwingUtil.class, |
27 |
"resource.locales.SwingResourceBundle"), Locale.ENGLISH); |
28 |
|
29 |
private Timer updateTimer; |
30 |
String valueAsText = ""; |
31 |
MbDecimalFormatter formatter = new MbDecimalFormatter(); |
32 |
|
33 |
public HeapBar() { |
34 |
updateTimer = new Timer("check memory"); |
35 |
updateTimer.scheduleAtFixedRate(new UpdateMemoryBarTask(), 100, 1000); |
36 |
|
37 |
setMinimum(0); |
38 |
setMaximum(100); |
39 |
|
40 |
SwingUtil.setPreferredWidth(this, 200); |
41 |
|
42 |
// Clicking the bar triggers a GC |
43 |
addMouseListener( new MouseAdapter() { |
44 |
@Override |
45 |
public void mouseClicked(MouseEvent e) { |
46 |
LangUtil.gcTotal(); |
47 |
} |
48 |
}); |
49 |
} |
50 |
|
51 |
@Override |
52 |
protected void paintComponent(Graphics g) { |
53 |
super.paintComponent(g); |
54 |
g.setColor(Color.black); |
55 |
g.drawString(valueAsText, 5, 12); |
56 |
} |
57 |
|
58 |
class UpdateMemoryBarTask extends TimerTask { |
59 |
|
60 |
@Override |
61 |
public void run() { |
62 |
|
63 |
// Get current size of heap in bytes |
64 |
long heapSize = Runtime.getRuntime().totalMemory(); |
65 |
|
66 |
// Get maximum size of heap in bytes. The heap cannot grow beyond |
67 |
// this size. |
68 |
// Any attempt will result in an OutOfMemoryException. |
69 |
long heapMaxSize = Runtime.getRuntime().maxMemory(); |
70 |
|
71 |
// Get amount of free memory within the heap in bytes. This size |
72 |
// will increase |
73 |
// after garbage collection and decrease as new objects are created. |
74 |
long heapFreeSize = Runtime.getRuntime().freeMemory(); |
75 |
|
76 |
long used = (heapSize - heapFreeSize); |
77 |
|
78 |
int percentaUsed = (int) (used * 100 / heapMaxSize); |
79 |
|
80 |
if (percentaUsed > 80) |
81 |
LOGGER.warn(percentaUsed + "% belegt"); |
82 |
|
83 |
if (percentaUsed > 70) { |
84 |
if (percentaUsed > 90) |
85 |
setForeground(Color.red); |
86 |
else |
87 |
setForeground(Color.orange); |
88 |
} else { |
89 |
setForeground(Color.green); |
90 |
} |
91 |
|
92 |
setValue(percentaUsed); |
93 |
valueAsText = RESOURCE.getString("HeapMemoryBar.status", formatter |
94 |
.format(used), percentaUsed, formatter.format(heapMaxSize)); |
95 |
|
96 |
repaint(); |
97 |
} |
98 |
} |
99 |
} |