|
1 | 1 | package net.hontheim; |
2 | 2 |
|
3 | 3 | import javafx.application.Application; |
| 4 | +import javafx.event.ActionEvent; |
| 5 | +import javafx.event.EventHandler; |
| 6 | +import javafx.geometry.Insets; |
| 7 | +import javafx.geometry.Pos; |
4 | 8 | import javafx.scene.Scene; |
| 9 | +import javafx.scene.control.Button; |
5 | 10 | import javafx.scene.control.Label; |
6 | 11 | import javafx.scene.layout.GridPane; |
7 | 12 | import javafx.stage.Stage; |
8 | 13 |
|
| 14 | +import java.io.IOException; |
| 15 | +import java.nio.ByteBuffer; |
| 16 | + |
9 | 17 | public class MainApp extends Application { |
10 | 18 |
|
| 19 | + private GridPane pane = new GridPane(); |
| 20 | + private Label lblTitle, lblDirectAllocation, lblNativeUsed, lblmaxMemory; |
| 21 | + private Label valDirectAllocation, valNativeUsed, valmaxMemory; |
| 22 | + private Button btnUpdate, btnReopen; |
| 23 | + |
| 24 | + private static ByteBuffer humonguosBuffer = ByteBuffer.allocateDirect(1024*1024*1024); |
| 25 | + |
11 | 26 | public static void main(String[] args) { |
12 | 27 | launch(args); |
13 | 28 | } |
14 | 29 |
|
15 | 30 | @Override |
16 | 31 | public void start(Stage stage) throws Exception { |
17 | | - stage.setScene(new Scene(new GridPane(), 160, 90)); |
| 32 | + stage.setTitle("Java Memory"); |
| 33 | + this.initializeComponents(); |
| 34 | + this.updateValues(); |
| 35 | + this.placeComponents(stage); |
| 36 | + this.registerListener(); |
| 37 | + stage.setResizable(false); |
18 | 38 | stage.show(); |
19 | 39 | } |
| 40 | + |
| 41 | + private void initializeComponents() { |
| 42 | + lblTitle = new Label("Java Memory"); |
| 43 | + lblTitle.setStyle("-fx-font-size: 18; -fx-font-weight: bold;"); |
| 44 | + |
| 45 | + lblDirectAllocation = new Label("Direct allocation:"); |
| 46 | + lblNativeUsed = new Label("Native memory used:"); |
| 47 | + lblmaxMemory = new Label("Max direct memory:"); |
| 48 | + |
| 49 | + valDirectAllocation = new Label(); |
| 50 | + valNativeUsed = new Label(); |
| 51 | + valmaxMemory = new Label(); |
| 52 | + |
| 53 | + btnUpdate = new Button("Update"); |
| 54 | + btnUpdate.setDefaultButton(true); |
| 55 | + } |
| 56 | + |
| 57 | + private void placeComponents(Stage stage) { |
| 58 | + pane.setHgap(10); |
| 59 | + pane.setVgap(10); |
| 60 | + pane.setPadding(new Insets(15, 25, 15, 25)); |
| 61 | + |
| 62 | + pane.add(lblTitle, 0, 0, 2, 1); |
| 63 | + |
| 64 | + pane.add(lblDirectAllocation, 0, 2); |
| 65 | + pane.add(lblNativeUsed, 0, 3); |
| 66 | + pane.add(lblmaxMemory, 0, 4); |
| 67 | + |
| 68 | + pane.add(valDirectAllocation, 1, 2); |
| 69 | + pane.add(valNativeUsed, 1, 3); |
| 70 | + pane.add(valmaxMemory, 1, 4); |
| 71 | + |
| 72 | + pane.add(btnUpdate, 0, 6); |
| 73 | + |
| 74 | + pane.setAlignment(Pos.CENTER); |
| 75 | + |
| 76 | + stage.setScene(new Scene(pane, 300, 250)); |
| 77 | + } |
| 78 | + |
| 79 | + private void registerListener() { |
| 80 | + btnUpdate.setOnAction(new EventHandler<ActionEvent>() { |
| 81 | + @Override |
| 82 | + public void handle(ActionEvent event) { |
| 83 | + updateValues(); |
| 84 | + } |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + private void updateValues() { |
| 89 | + this.valDirectAllocation.setText("" + humonguosBuffer.capacity()); |
| 90 | + this.valNativeUsed.setText("" + sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getMemoryUsed()); |
| 91 | + this.valmaxMemory.setText("" + sun.misc.VM.maxDirectMemory()); |
| 92 | + } |
20 | 93 | } |
0 commit comments