1 |
alfonx |
519 |
package skrueger.swing; |
2 |
|
|
|
3 |
|
|
import java.awt.Color; |
4 |
|
|
import java.awt.Graphics; |
5 |
|
|
import java.util.Locale; |
6 |
|
|
import java.util.Timer; |
7 |
|
|
import java.util.TimerTask; |
8 |
|
|
|
9 |
|
|
import javax.swing.JProgressBar; |
10 |
|
|
import javax.swing.SwingUtilities; |
11 |
|
|
|
12 |
|
|
import org.apache.log4j.Logger; |
13 |
|
|
|
14 |
|
|
import schmitzm.geotools.gui.GeotoolsGUIUtil; |
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 |
|
|
} |
40 |
|
|
|
41 |
|
|
@Override |
42 |
|
|
protected void paintComponent(Graphics g) { |
43 |
|
|
super.paintComponent(g); |
44 |
|
|
g.setColor(Color.black); |
45 |
|
|
g.drawString(valueAsText, 5, 12); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
class UpdateMemoryBarTask extends TimerTask { |
49 |
|
|
|
50 |
|
|
@Override |
51 |
|
|
public void run() { |
52 |
|
|
|
53 |
|
|
// Get current size of heap in bytes |
54 |
|
|
long heapSize = Runtime.getRuntime().totalMemory(); |
55 |
|
|
|
56 |
|
|
// Get maximum size of heap in bytes. The heap cannot grow beyond |
57 |
|
|
// this size. |
58 |
|
|
// Any attempt will result in an OutOfMemoryException. |
59 |
|
|
long heapMaxSize = Runtime.getRuntime().maxMemory(); |
60 |
|
|
|
61 |
|
|
// Get amount of free memory within the heap in bytes. This size |
62 |
|
|
// will increase |
63 |
|
|
// after garbage collection and decrease as new objects are created. |
64 |
|
|
long heapFreeSize = Runtime.getRuntime().freeMemory(); |
65 |
|
|
|
66 |
|
|
long used = (heapSize - heapFreeSize); |
67 |
|
|
|
68 |
|
|
int percentaUsed = (int) (used * 100 / heapMaxSize); |
69 |
|
|
|
70 |
|
|
if (percentaUsed > 80) |
71 |
|
|
LOGGER.warn(percentaUsed + "% belegt"); |
72 |
|
|
|
73 |
|
|
if (percentaUsed > 70) { |
74 |
|
|
if (percentaUsed > 90) |
75 |
|
|
setForeground(Color.red); |
76 |
|
|
else |
77 |
|
|
setForeground(Color.orange); |
78 |
|
|
} else { |
79 |
|
|
setForeground(Color.green); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
setValue(percentaUsed); |
83 |
|
|
valueAsText = RESOURCE.getString("HeapMemoryBar.status", formatter |
84 |
|
|
.format(used), percentaUsed, formatter.format(heapMaxSize)); |
85 |
|
|
|
86 |
|
|
repaint(); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
} |