/[schmitzm]/trunk/src/skrueger/swing/HeapBar.java
ViewVC logotype

Contents of /trunk/src/skrueger/swing/HeapBar.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 674 - (show annotations)
Thu Feb 4 16:28:15 2010 UTC (15 years ago) by alfonx
Original Path: branches/2.0-RC2/src/skrueger/swing/HeapBar.java
File MIME type: text/plain
File size: 2532 byte(s)
Calculating Chart statistics on extra threads (not EDT)
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
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(), 1000, 5000);
34
35 setMinimum(0);
36 setMaximum(100);
37
38 SwingUtil.setPreferredWidth(this, 200);
39
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 }
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 }

Properties

Name Value
svn:eol-style native
svn:keywords Id URL
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26