-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
HeapUsageBar.java
68 lines (53 loc) · 1.37 KB
/
HeapUsageBar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package jadx.gui.ui;
import jadx.gui.utils.NLS;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HeapUsageBar extends JProgressBar implements ActionListener {
private static final double TWO_TO_20 = 1048576d; // 1024 * 1024
private final Color GREEN = new Color(0, 180, 0);
private final Color RED = new Color(200, 0, 0);
private final Runtime r;
private String maxHeapStr;
private final Timer timer;
private final double maxGB;
private final String textFormat;
public HeapUsageBar() {
super();
textFormat = NLS.str("heapUsage.text");
r = Runtime.getRuntime();
setBorderPainted(false);
setStringPainted(true);
setValue(10);
int maxKB = (int) (r.maxMemory() / 1024);
setMaximum(maxKB);
maxGB = maxKB / TWO_TO_20;
update();
timer = new Timer(1000, this);
}
public void update() {
long used = r.totalMemory() - r.freeMemory();
int usedKB = (int) (used / 1024);
setValue(usedKB);
setString(String.format(textFormat, (usedKB / TWO_TO_20), maxGB));
if (used > r.totalMemory() * 0.8) {
setForeground(RED);
} else {
setForeground(GREEN);
}
}
@Override
public void actionPerformed(ActionEvent e) {
update();
}
@Override
public void setVisible(boolean aFlag) {
super.setVisible(aFlag);
if (aFlag) {
timer.start();
} else {
timer.stop();
}
}
}