Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Systemsoftware - Abgabe Aufgabenblatt 1 #4

Open
wants to merge 1 commit into
base: 2023w
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Auswertungen/A4-memoryLog_2023-11-19T12:39:06.615760.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Gesammtbelegung,"Freier Speicher", "Heap Speicher"
133120,130219,2901
1479680,513632,966048
2097152,160350,1936802
2097152,54203,2042949
2097152,54201,2042951
6 changes: 6 additions & 0 deletions Auswertungen/A5.1-memoryLog_2023-11-19T12:44:38.970616.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Gesammtbelegung,"Freier Speicher", "Heap Speicher"
133120,130219,2901
133120,75128,57992
133120,78200,54920
133120,78198,54922
133120,78198,54922
6 changes: 6 additions & 0 deletions Auswertungen/A5.2-memoryLog_2023-11-19T12:46:28.882046.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Gesammtbelegung,"Freier Speicher", "Heap Speicher"
133120,130219,2901
133120,86894,46226
133120,78190,54930
133120,77691,55429
133120,77691,55429
Binary file added Auswertungen/Auswertung A4+5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Auswertungen/Auswertung A5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions JavaCountLines/src/FileGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.*;
import java.lang.Math;

public class FileGenerator{

private String charString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
private long currentSize = 0;

public FileGenerator(double filesize, File f) throws IOException{
if (filesize <= 0) throw new IllegalArgumentException("File size must be greater than 0");
long size = (long) (filesize * Math.pow( 1.074*10 , 9));

BufferedWriter writer = new BufferedWriter(new FileWriter(f));
try {
while (currentSize < size){
int lineLength = (int)(Math.random() * 1000);
String line = "";
for (int i = 0; i < lineLength; i++){
line += charString.charAt((int)(Math.random() * charString.length()));
}
writer.write(line);
writer.newLine();
currentSize += 2 * lineLength;
}
} catch (Exception e){
System.out.println(e);
}
if (writer != null) writer.close();
}
}
48 changes: 47 additions & 1 deletion JavaCountLines/src/Main.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.*;
Expand All @@ -9,7 +10,52 @@ public class Main {
public static void main(String[] args) throws IOException {
assert(args.length == 2);
// Regular expression ".*\.c$" matches any c source files
System.out.println(countLinesInAllFiles(args[0],args[1]));
//System.out.println(countLinesInAllFiles(args[0],args[1]));

final String fileName = "New1GBFile.txt";
File f = new File(fileName);
FileGenerator fg;
if (!f.exists() && !f.isDirectory()) fg = new FileGenerator(1d, f);

MemoryLogManager mm = new MemoryLogManager(5,true);
mm.start();

/*
long startTime = System.currentTimeMillis();

//Aufgabe 4
//Laufzeit: 2708ms, sehr hohe Speicherauslastung.
long lineCount = countLines(f.getPath());

long endTime = System.currentTimeMillis();

System.out.println(lineCount + "lines in: " + (endTime-startTime) + "ms");
*/


//Aufgabe 5

long startTime = System.currentTimeMillis();
long lineCount = 0;

//5.1 Implementation durch BufferedReader
//Laufzeit: 1545ms, deutliche Reduktion der Speicherauslastung im Vergleich zu 4.
/*
BufferedReader bf = new BufferedReader(new FileReader(f));
while (bf.readLine() != null) lineCount++;
*/

//5.2 Implementation mit Files.lines und Stream
//Laufzeit: 1456ms, minimale reduktion der Speicherauslastung zu Beginn. Anschließend nahezu identisch (Siehe Logs/Graphen) verglichen mit 5.1. Dennoch großer Unteschied zu 4
try {
Stream<String> stream = Files.lines(Path.of(f.getPath()));
lineCount = (int) stream.count();
stream.close();
} catch (IOException e) {System.out.println(e);}


long endTime = System.currentTimeMillis();
System.out.println(lineCount + "lines in: " + (endTime-startTime) + "ms");
}

public static long countLines(String fileName) throws IOException {
Expand Down
70 changes: 70 additions & 0 deletions JavaCountLines/src/MemoryLogManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import java.time.LocalDateTime;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.File;

public class MemoryLogManager{

private int logCount;
private Timer timer;
private BufferedWriter writer;
private boolean silent;


public MemoryLogManager(int logCount, boolean silent) {
this.logCount = logCount;
this.silent = silent;
}

public void start(){
try {
writer = new BufferedWriter(new FileWriter(new File("memoryLog_"+ LocalDateTime.now() +".csv")));
writer.write("Gesammtbelegung,\"Freier Speicher\", \"Heap Speicher\"\n");

} catch (IOException e){
System.out.println(e);
}

timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
logMemory();
}
}, 0, 1000);
}

private void logMemory(){
logCount--;
if (logCount < 0){
timer.cancel();

try {
writer.close();
} catch (IOException e){
System.out.println(e);
}
System.out.println("Timer Finished");
return;
}

long totalMem = Runtime.getRuntime().totalMemory() / 1024;
long freeMem = Runtime.getRuntime().freeMemory() / 1024;
long usedMem = totalMem - freeMem;

if (!silent)
System.out.println("Total Memory: " + totalMem + " KB\n" +
"Free Memory: " + freeMem + " KB\n" +
"Used Memory: " + usedMem + " KB\n"
);

try {
writer.write(totalMem+","+freeMem+","+usedMem+System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
}
}