Commit 75554dbe by Patryk Czarnik

Przykłady XML - dodanie pomiaru pamięci po wymuszonym GC

parent a8535eb0
...@@ -47,12 +47,14 @@ public class Waluty1_Dom { ...@@ -47,12 +47,14 @@ public class Waluty1_Dom {
} }
long t3 = System.nanoTime(); long t3 = System.nanoTime();
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long pamiec = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("Czas czytania pliku: %.6f s%n", (t2-t1) * 1e-9); System.out.printf("Czas czytania pliku: %.6f s%n", (t2-t1) * 1e-9);
System.out.printf("Czas liczenia : %.6f s%n", (t3-t2) * 1e-9); System.out.printf("Czas liczenia : %.6f s%n", (t3-t2) * 1e-9);
System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9); System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9);
System.out.printf("Zajęta pamięć : %,d B%n", pamiec); System.out.printf("Zajęta pamięć : %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.gc();
Thread.sleep(1000);
System.out.printf("Zajęta pamięć po gc: %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.out.println("Count: " + count); System.out.println("Count: " + count);
if(count > 0) { if(count > 0) {
System.out.println("Avg: " + (sum / count)); System.out.println("Avg: " + (sum / count));
......
...@@ -52,12 +52,14 @@ public class Waluty2_XPath { ...@@ -52,12 +52,14 @@ public class Waluty2_XPath {
} }
long t3 = System.nanoTime(); long t3 = System.nanoTime();
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long pamiec = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("Czas czytania pliku: %.6f s%n", (t2-t1) * 1e-9); System.out.printf("Czas czytania pliku: %.6f s%n", (t2-t1) * 1e-9);
System.out.printf("Czas liczenia : %.6f s%n", (t3-t2) * 1e-9); System.out.printf("Czas liczenia : %.6f s%n", (t3-t2) * 1e-9);
System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9); System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9);
System.out.printf("Zajęta pamięć : %,d B%n", pamiec); System.out.printf("Zajęta pamięć : %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.gc();
Thread.sleep(1000);
System.out.printf("Zajęta pamięć po gc: %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.out.println("Count: " + count); System.out.println("Count: " + count);
if(count > 0) { if(count > 0) {
System.out.println("Sum: " + sum); System.out.println("Sum: " + sum);
......
...@@ -44,10 +44,12 @@ public class Waluty3_XPath { ...@@ -44,10 +44,12 @@ public class Waluty3_XPath {
} }
long t3 = System.nanoTime(); long t3 = System.nanoTime();
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long pamiec = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9); System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9);
System.out.printf("Zajęta pamięć : %,d B%n", pamiec); System.out.printf("Zajęta pamięć : %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.gc();
Thread.sleep(1000);
System.out.printf("Zajęta pamięć po gc: %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.out.println("Count: " + count); System.out.println("Count: " + count);
if(count > 0) { if(count > 0) {
System.out.println("Sum: " + sum); System.out.println("Sum: " + sum);
......
...@@ -35,12 +35,14 @@ public class Waluty3a_XPath_TylkoSuma { ...@@ -35,12 +35,14 @@ public class Waluty3a_XPath_TylkoSuma {
long t3 = System.nanoTime(); long t3 = System.nanoTime();
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long pamiec = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("Czas czytania pliku: %.6f s%n", (t2-t1) * 1e-9); System.out.printf("Czas czytania pliku: %.6f s%n", (t2-t1) * 1e-9);
System.out.printf("Czas liczenia : %.6f s%n", (t3-t2) * 1e-9); System.out.printf("Czas liczenia : %.6f s%n", (t3-t2) * 1e-9);
System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9); System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9);
System.out.printf("Zajęta pamięć : %,d B%n", pamiec); System.out.printf("Zajęta pamięć : %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.gc();
Thread.sleep(1000);
System.out.printf("Zajęta pamięć po gc: %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.out.println("Sum: " + sum); System.out.println("Sum: " + sum);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -5,7 +5,12 @@ import javax.xml.stream.XMLInputFactory; ...@@ -5,7 +5,12 @@ import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent; import javax.xml.stream.events.XMLEvent;
import java.io.FileReader; import java.io.FileReader;
public class Waluty4_Stax { /* Wersja oparta o StAX EventReader - dokument nie jest wczytywany do pamięci "na raz",
ale w każdym kroku tworzony jest (zwykle b. mały) obiekt Event (różnego typu),
co powoduje, że wydajność nie jest jeszcze optymalna,
ale za to programuje się nieco wygodniej, niż w następnej wersji.
*/
public class Waluty4_StaxEvent {
public static void main(String[] args) { public static void main(String[] args) {
String plik = "pliki/waluty2023.xml"; String plik = "pliki/waluty2023.xml";
...@@ -30,7 +35,7 @@ public class Waluty4_Stax { ...@@ -30,7 +35,7 @@ public class Waluty4_Stax {
if(waluta.equals(reader.getElementText())) { if(waluta.equals(reader.getElementText())) {
uwzgledniajWalute = true; uwzgledniajWalute = true;
} }
} } else
if(event2.isStartElement() && event2.asStartElement().getName().getLocalPart().equals("Mid")) { if(event2.isStartElement() && event2.asStartElement().getName().getLocalPart().equals("Mid")) {
if(uwzgledniajWalute) { if(uwzgledniajWalute) {
double mid = Double.parseDouble(reader.getElementText()); double mid = Double.parseDouble(reader.getElementText());
...@@ -39,7 +44,7 @@ public class Waluty4_Stax { ...@@ -39,7 +44,7 @@ public class Waluty4_Stax {
if(mid < min) min = mid; if(mid < min) min = mid;
if(mid > max) max = mid; if(mid > max) max = mid;
} }
} } else
if(event2.isEndElement() && event2.asEndElement().getName().getLocalPart().equals("Rate")) { if(event2.isEndElement() && event2.asEndElement().getName().getLocalPart().equals("Rate")) {
break; break;
} }
...@@ -49,10 +54,12 @@ public class Waluty4_Stax { ...@@ -49,10 +54,12 @@ public class Waluty4_Stax {
long t3 = System.nanoTime(); long t3 = System.nanoTime();
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long pamiec = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9); System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9);
System.out.printf("Zajęta pamięć : %,d B%n", pamiec); System.out.printf("Zajęta pamięć : %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.gc();
Thread.sleep(1000);
System.out.printf("Zajęta pamięć po gc: %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.out.println("Count: " + count); System.out.println("Count: " + count);
if(count > 0) { if(count > 0) {
System.out.println("Sum: " + sum); System.out.println("Sum: " + sum);
......
...@@ -4,7 +4,10 @@ import javax.xml.stream.XMLStreamReader; ...@@ -4,7 +4,10 @@ import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLInputFactory;
import java.io.FileReader; import java.io.FileReader;
public class Waluty5_Stax { /* Wersja oparta o StAX StreamReader - w tym rozwiązaniu nie są tworzone obiekty Event dla każdego kroku,
dzięki czemu jest to bardziej wydajne (gł pamięciowo), niż wersja poprzednia.
*/
public class Waluty5_StaxStream {
public static void main(String[] args) { public static void main(String[] args) {
String plik = "pliki/waluty2023.xml"; String plik = "pliki/waluty2023.xml";
...@@ -29,7 +32,7 @@ public class Waluty5_Stax { ...@@ -29,7 +32,7 @@ public class Waluty5_Stax {
if(waluta.equals(reader.getElementText())) { if(waluta.equals(reader.getElementText())) {
uwzgledniajWalute = true; uwzgledniajWalute = true;
} }
} } else
if(reader.isStartElement() && reader.getName().getLocalPart().equals("Mid")) { if(reader.isStartElement() && reader.getName().getLocalPart().equals("Mid")) {
if(uwzgledniajWalute) { if(uwzgledniajWalute) {
double mid = Double.parseDouble(reader.getElementText()); double mid = Double.parseDouble(reader.getElementText());
...@@ -38,7 +41,7 @@ public class Waluty5_Stax { ...@@ -38,7 +41,7 @@ public class Waluty5_Stax {
if(mid < min) min = mid; if(mid < min) min = mid;
if(mid > max) max = mid; if(mid > max) max = mid;
} }
} } else
if(reader.isEndElement() && reader.getName().getLocalPart().equals("Rate")) { if(reader.isEndElement() && reader.getName().getLocalPart().equals("Rate")) {
break; break;
} }
...@@ -48,10 +51,12 @@ public class Waluty5_Stax { ...@@ -48,10 +51,12 @@ public class Waluty5_Stax {
long t3 = System.nanoTime(); long t3 = System.nanoTime();
Runtime runtime = Runtime.getRuntime(); Runtime runtime = Runtime.getRuntime();
long pamiec = runtime.totalMemory() - runtime.freeMemory();
System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9); System.out.printf("Czas łączny : %.6f s%n", (t3-t1) * 1e-9);
System.out.printf("Zajęta pamięć : %,d B%n", pamiec); System.out.printf("Zajęta pamięć : %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.gc();
Thread.sleep(1000);
System.out.printf("Zajęta pamięć po gc: %,d B%n", runtime.totalMemory() - runtime.freeMemory());
System.out.println("Count: " + count); System.out.println("Count: " + count);
if(count > 0) { if(count > 0) {
System.out.println("Sum: " + sum); System.out.println("Sum: " + sum);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment