Java Code Samples

 import java.util.HashMap;


public class LeakingClass {


    private HashMap<String,String> alongForTheRide = new HashMap();

    private String alsoAlongForTheRide;

    private LeakingField leakingField;


    public LeakingClass(String parameter, LeakingField field) {

        alsoAlongForTheRide = parameter;

        leakingField = field;

    }


    public String toString() {

        StringBuffer buffer = new StringBuffer(alongForTheRide.toString());

        buffer.append(alsoAlongForTheRide);

        buffer.append(leakingField);

        return buffer.toString();

    }

}


public class LeakingField {


    private String string = "more string stuff to leak";


    public LeakingField() {}


    public String toString() {

        return string;

    }

}


import javafx.animation.KeyFrame;

import javafx.animation.Timeline;

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.chart.NumberAxis;

import javafx.scene.chart.ScatterChart;

import javafx.scene.chart.XYChart;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.layout.HBox;

import javafx.scene.layout.VBox;

import javafx.stage.Stage;

import javafx.util.Duration;


import java.lang.management.ManagementFactory;


public class Leaky extends Application {


    private LeakyModel model;

    private ScatterChart<Number, Number> chart;

    private XYChart.Series<Number,Number> heapOccupancy = new XYChart.Series<>();

    private long baseTime = System.currentTimeMillis();


    @Override

    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("Leaky");

        model = new LeakyModel();


        chart = buildScatterChart(

                "Memory Use",

                "Time (seconds)",

                "Occupancy (K)");


        TextField integerField = new TextField("1000000");


        Button button = new Button("Do Stuff");

        button.setOnAction((event)-> model.leak(Integer.valueOf(integerField.getText())));


        HBox controls = new HBox(5, integerField, button);

        VBox root = new VBox(5, controls, chart);

        root.setAlignment(Pos.CENTER);

        Scene scene = new Scene(root, 900, 360);

        primaryStage.setScene(scene);

        primaryStage.show();


        Timeline timeline = new Timeline();

        timeline.getKeyFrames().add(

                new KeyFrame(Duration.millis(1000), (ActionEvent actionEvent) -> {

                    Number currentHeapOccupancy = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed() / 1024;

                    Number currentTimeSeconds = (double) (System.currentTimeMillis() - baseTime) / 1000.0d;

                    heapOccupancy.getData().add(new XYChart.Data<>(currentTimeSeconds,currentHeapOccupancy));

                }));

        timeline.setCycleCount(1000);

        timeline.setAutoReverse(true);  //!?

        timeline.play();

    }


    ScatterChart<Number,Number> buildScatterChart(String title, String xAxisLabel, String yAxisLabel) { //, ) { //Map<SafepointCause,ArrayList<DataPoint>> seriesData) {

        NumberAxis xAxis = new NumberAxis();

        xAxis.setLabel(xAxisLabel);

        NumberAxis yAxis = new NumberAxis();

        yAxis.setLabel(yAxisLabel);

        ScatterChart<Number,Number> chart = new ScatterChart<>(xAxis,yAxis);

        chart.setTitle(title);

        heapOccupancy.setName("Heap Occupancy");

        chart.getData().add(heapOccupancy);

        return chart;

    }

}

import java.util.ArrayList;


public class LeakyModel {

    private ArrayList<LeakingClass> leaker;

    public LeakyModel() {
        leaker = new ArrayList<>();
    }

    public void leak(int range) {
        char[] aString = { 's','o','m','e',' ','l','e','a','k','i','n','g',' ','s','t','u','f','f'};
        for ( int i = 0; i < range; i++) {
            leaker.add(new LeakingClass(new String(aString), new LeakingField()));
        }
    }
}

Post a Comment

0 Comments