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