-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
66 lines (59 loc) · 2.92 KB
/
Copy pathMain.java
File metadata and controls
66 lines (59 loc) · 2.92 KB
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
import java.io.*;
import java.util.concurrent.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Memory Log Output");
JTextArea textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new java.awt.Dimension(500, 300));
frame.add(scrollPane);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
new Thread(() -> {
try {
File logFile = new File("memory.log");
if (!logFile.exists()) {
logFile.createNewFile();
}
Process compile = new ProcessBuilder("javac", "Memorychecker.java", "MemoryAnanlyzer.java").start();
compile.waitFor();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable task = () -> {
try {
Process process = new ProcessBuilder("java", "Memorychecker").start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
String finalLine = line;
SwingUtilities.invokeLater(() -> textArea.append(finalLine + "\n"));
if (line.startsWith("CSV_LOG:")) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("memory.log", true))) {
String csvLine = line.replace("CSV_LOG: ", "").trim();
writer.write(csvLine);
writer.newLine();
} catch (IOException e) {
System.err.println("Failed to write to memory.log: " + e.getMessage());
}
}
}
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
};
scheduler.scheduleAtFixedRate(task, 0, 5, TimeUnit.SECONDS);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
scheduler.shutdownNow();
}
});
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}