11package net .hontheim ;
22
33import javafx .application .Application ;
4- import javafx .event .ActionEvent ;
5- import javafx .event .EventHandler ;
64import javafx .geometry .Insets ;
75import javafx .geometry .Pos ;
86import javafx .scene .Scene ;
119import javafx .scene .layout .GridPane ;
1210import javafx .stage .Stage ;
1311
14- import java .nio .ByteBuffer ;
15-
1612public class MainApp extends Application {
1713
18- private GridPane pane = new GridPane ();
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 ;
14+ private final GridPane pane = new GridPane ();
15+ private Label lblTitle , lblRuntimeMax , lblRuntimeFree , lblRuntimeTotal ;
16+ private Label descRuntimeMax , descRuntimeFree , descRuntimeTotal ;
17+ private Label valRuntimeMax , valRuntimeFree , valRuntimeTotal ;
2218 private Button btnUpdate ;
2319
24- private static ByteBuffer buffer = ByteBuffer .allocateDirect (1024 *1024 *1024 );
25-
2620 public static void main (String [] args ) {
2721 launch (args );
2822 }
2923
3024 @ Override
31- public void start (Stage stage ) throws Exception {
25+ public void start (Stage stage ) {
3226 stage .setTitle ("Java Memory" );
3327 this .initializeComponents ();
3428 this .placeComponents (stage );
@@ -41,21 +35,6 @@ private void initializeComponents() {
4135 lblTitle = new Label ("Java Memory" );
4236 lblTitle .setStyle ("-fx-font-size: 18; -fx-font-weight: bold;" );
4337
44- lblDirectAllocation = new Label ("Direct allocation:" );
45- lblNativeUsed = new Label ("Native memory used:" );
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" );
54-
55- valDirectAllocation = new Label ();
56- valNativeUsed = new Label ();
57- valMaxMemory = new Label ();
58-
5938 lblRuntimeMax = new Label ("Runtime max memory:" );
6039 lblRuntimeFree = new Label ("Runtime free memory:" );
6140 lblRuntimeTotal = new Label ("Runtime total memory:" );
@@ -82,51 +61,30 @@ private void placeComponents(Stage stage) {
8261
8362 pane .add (lblTitle , 0 , 0 , 2 , 1 );
8463
85- pane .add (lblDirectAllocation , 0 , 2 );
86- pane .add (lblNativeUsed , 0 , 3 );
87- pane .add (lblMaxMemory , 0 , 4 );
64+ pane .add (lblRuntimeMax , 0 , 2 );
65+ pane .add (lblRuntimeFree , 0 , 3 );
66+ pane .add (lblRuntimeTotal , 0 , 4 );
8867
89- pane .add (descDirectAllocation , 1 , 2 );
90- pane .add (descNativeUsed , 1 , 3 );
91- pane .add (descMaxMemory , 1 , 4 );
68+ pane .add (descRuntimeMax , 1 , 2 );
69+ pane .add (descRuntimeFree , 1 , 3 );
70+ pane .add (descRuntimeTotal , 1 , 4 );
9271
93- pane .add (valDirectAllocation , 2 , 2 );
94- pane .add (valNativeUsed , 2 , 3 );
95- pane .add (valMaxMemory , 2 , 4 );
72+ pane .add (valRuntimeMax , 2 , 2 );
73+ pane .add (valRuntimeFree , 2 , 3 );
74+ pane .add (valRuntimeTotal , 2 , 4 );
9675
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 );
76+ pane .add (btnUpdate , 0 , 6 );
11077
11178 pane .setAlignment (Pos .CENTER );
11279
113- stage .setScene (new Scene (pane , 900 , 375 ));
80+ stage .setScene (new Scene (pane , 600 , 300 ));
11481 }
11582
11683 private void registerListener () {
117- btnUpdate .setOnAction (new EventHandler <ActionEvent >() {
118- @ Override
119- public void handle (ActionEvent event ) {
120- updateValues ();
121- }
122- });
84+ btnUpdate .setOnAction (event -> updateValues ());
12385 }
12486
12587 private void updateValues () {
126- this .valDirectAllocation .setText ("" + buffer .capacity ());
127- this .valNativeUsed .setText ("" + sun .misc .SharedSecrets .getJavaNioAccess ().getDirectBufferPool ().getMemoryUsed ());
128- this .valMaxMemory .setText ("" + sun .misc .VM .maxDirectMemory ());
129-
13088 this .valRuntimeMax .setText ("" + Runtime .getRuntime ().maxMemory ());
13189 this .valRuntimeFree .setText ("" + Runtime .getRuntime ().freeMemory ());
13290 this .valRuntimeTotal .setText ("" + Runtime .getRuntime ().totalMemory ());
0 commit comments