1111import javafx .scene .layout .GridPane ;
1212import javafx .stage .Stage ;
1313
14- import java .io .IOException ;
1514import java .nio .ByteBuffer ;
1615
1716public class MainApp extends Application {
1817
1918 private GridPane pane = new GridPane ();
20- private Label lblTitle , lblDirectAllocation , lblNativeUsed , lblmaxMemory ;
21- private Label valDirectAllocation , valNativeUsed , valmaxMemory ;
22- private Button btnUpdate , btnReopen ;
19+ private Label lblTitle , lblDirectAllocation , lblNativeUsed , lblMaxMemory , lblRuntimeMax , lblRuntimeFree , lblRuntimeTotal ;
20+ private Label descDirectAllocation , descNativeUsed , descMaxMemory , descRuntimeMax , descRuntimeFree , descRuntimeTotal ;
21+ private Label valDirectAllocation , valNativeUsed , valMaxMemory , valRuntimeMax , valRuntimeFree , valRuntimeTotal ;
22+ private Button btnUpdate ;
2323
24- private static ByteBuffer humonguosBuffer = ByteBuffer .allocateDirect (1024 *1024 *1024 );
24+ private static ByteBuffer buffer = ByteBuffer .allocateDirect (1024 *1024 *1024 );
2525
2626 public static void main (String [] args ) {
2727 launch (args );
@@ -31,11 +31,10 @@ public static void main(String[] args) {
3131 public void start (Stage stage ) throws Exception {
3232 stage .setTitle ("Java Memory" );
3333 this .initializeComponents ();
34- this .updateValues ();
3534 this .placeComponents (stage );
3635 this .registerListener ();
37- stage .setResizable (false );
3836 stage .show ();
37+ this .updateValues ();
3938 }
4039
4140 private void initializeComponents () {
@@ -44,11 +43,33 @@ private void initializeComponents() {
4443
4544 lblDirectAllocation = new Label ("Direct allocation:" );
4645 lblNativeUsed = new Label ("Native memory used:" );
47- lblmaxMemory = new Label ("Max direct memory:" );
46+ lblMaxMemory = new Label ("Max direct memory:" );
47+
48+ descDirectAllocation = new Label ("ByteBuffer.capacity();" );
49+ descDirectAllocation .setStyle ("-fx-font-family: monospace" );
50+ descNativeUsed = new Label ("sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getMemoryUsed();" );
51+ descNativeUsed .setStyle ("-fx-font-family: monospace" );
52+ descMaxMemory = new Label ("sun.misc.VM.maxDirectMemory();" );
53+ descMaxMemory .setStyle ("-fx-font-family: monospace" );
4854
4955 valDirectAllocation = new Label ();
5056 valNativeUsed = new Label ();
51- valmaxMemory = new Label ();
57+ valMaxMemory = new Label ();
58+
59+ lblRuntimeMax = new Label ("Runtime max memory:" );
60+ lblRuntimeFree = new Label ("Runtime free memory:" );
61+ lblRuntimeTotal = new Label ("Runtime total memory:" );
62+
63+ descRuntimeMax = new Label ("Runtime.getRuntime().maxMemory();" );
64+ descRuntimeMax .setStyle ("-fx-font-family: monospace" );
65+ descRuntimeFree = new Label ("Runtime.getRuntime().freeMemory();" );
66+ descRuntimeFree .setStyle ("-fx-font-family: monospace" );
67+ descRuntimeTotal = new Label ("Runtime.getRuntime().totalMemory();" );
68+ descRuntimeTotal .setStyle ("-fx-font-family: monospace" );
69+
70+ valRuntimeMax = new Label ();
71+ valRuntimeFree = new Label ();
72+ valRuntimeTotal = new Label ();
5273
5374 btnUpdate = new Button ("Update" );
5475 btnUpdate .setDefaultButton (true );
@@ -63,17 +84,33 @@ private void placeComponents(Stage stage) {
6384
6485 pane .add (lblDirectAllocation , 0 , 2 );
6586 pane .add (lblNativeUsed , 0 , 3 );
66- pane .add (lblmaxMemory , 0 , 4 );
87+ pane .add (lblMaxMemory , 0 , 4 );
6788
68- pane .add (valDirectAllocation , 1 , 2 );
69- pane .add (valNativeUsed , 1 , 3 );
70- pane .add (valmaxMemory , 1 , 4 );
89+ pane .add (descDirectAllocation , 1 , 2 );
90+ pane .add (descNativeUsed , 1 , 3 );
91+ pane .add (descMaxMemory , 1 , 4 );
7192
72- pane .add (btnUpdate , 0 , 6 );
93+ pane .add (valDirectAllocation , 2 , 2 );
94+ pane .add (valNativeUsed , 2 , 3 );
95+ pane .add (valMaxMemory , 2 , 4 );
96+
97+ pane .add (lblRuntimeMax , 0 , 6 );
98+ pane .add (lblRuntimeFree , 0 , 7 );
99+ pane .add (lblRuntimeTotal , 0 , 8 );
100+
101+ pane .add (descRuntimeMax , 1 , 6 );
102+ pane .add (descRuntimeFree , 1 , 7 );
103+ pane .add (descRuntimeTotal , 1 , 8 );
104+
105+ pane .add (valRuntimeMax , 2 , 6 );
106+ pane .add (valRuntimeFree , 2 , 7 );
107+ pane .add (valRuntimeTotal , 2 , 8 );
108+
109+ pane .add (btnUpdate , 0 , 10 );
73110
74111 pane .setAlignment (Pos .CENTER );
75112
76- stage .setScene (new Scene (pane , 300 , 250 ));
113+ stage .setScene (new Scene (pane , 900 , 375 ));
77114 }
78115
79116 private void registerListener () {
@@ -86,8 +123,12 @@ public void handle(ActionEvent event) {
86123 }
87124
88125 private void updateValues () {
89- this .valDirectAllocation .setText ("" + humonguosBuffer .capacity ());
126+ this .valDirectAllocation .setText ("" + buffer .capacity ());
90127 this .valNativeUsed .setText ("" + sun .misc .SharedSecrets .getJavaNioAccess ().getDirectBufferPool ().getMemoryUsed ());
91- this .valmaxMemory .setText ("" + sun .misc .VM .maxDirectMemory ());
128+ this .valMaxMemory .setText ("" + sun .misc .VM .maxDirectMemory ());
129+
130+ this .valRuntimeMax .setText ("" + Runtime .getRuntime ().maxMemory ());
131+ this .valRuntimeFree .setText ("" + Runtime .getRuntime ().freeMemory ());
132+ this .valRuntimeTotal .setText ("" + Runtime .getRuntime ().totalMemory ());
92133 }
93134}
0 commit comments