Der Java-Experten-Kurs

Werbung
Der Java-Experten-Kurs
Speicherbelegung und Laufzeitberechnung
in Java
Karl Pauls
Abgabe
• <session>_<gruppe>.jar
– gruppe/
•
•
•
•
doc.pdf
build.xml
src
Classes
• z.B.: Gruppe 2 Session 2
– 2_2.zip ->
• 2/
–
–
–
–
doc.pdf
build.xml
Src
classes
build.xml
<project name="2_0" default="all" basedir=".">
<property name="group" value="0"/>
<property name="session" value="2"/>
<target name="init">
<mkdir dir="classes"/>
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="classes" debug="on"
verbose="no">
<include name="**/*.java"/>
</javac>
</target>
<target name="run">
build.xml
<java classpath="classes" classname="s2.g0.Main" fork="true">
<jvmarg value="-Xmx512m"/>
</java>
</target>
<target name="all">
<antcall target="clean"/>
<antcall target="init" />
<antcall target="compile"/>
<antcall target="run"/>
</target>
<target name="clean" >
<delete dir="classes"/>
<delete dir="doc"/>
</target>
build.xml
<target name="dist">
<mkdir dir="doc"/>
<javadoc sourcepath="src” packagenames="*”
destdir="doc" author="true"
windowtitle="Java Experten Kurs - (${session}/${group})"/>
<jar jarfile="../${session}_${group}.jar" basedir="..">
<include name="${group}/doc.pdf"/>
<include name="${group}/src/**"/>
<include name="${group}/doc/**"/>
<include name="${group}/build.xml"/>
</jar>
</target>
Abgabe
• package s<session>.g<group>
– z.B.: s2.g0
• Mail
– jep <session> <group>
– z.B.:
– jep 2 2
Speicherbelegung in Java
1. Exemplar benötigt 8 bytes: new Object() -> 8 bytes.
2. Primitive brauchen 4 bytes, long und double 8.
byte braucht ebenfalls 4 bytes.
3. Speicherbelegung erfolgt in 8 byte Schritten.
-> 8 bytes für Klasse und 8 bytes -> 16 bytes.
4. Arrays sind anders.
5. Pointer
class MemorySizes
public class MemorySizes {
private final Map primitiveSizes = new IdentityHashMap() {
{
put(boolean.class, new Integer(1));
put(byte.class, new Integer(1));
put(char.class, new Integer(2));
put(short.class, new Integer(2));
put(int.class, new Integer(4));
put(float.class, new Integer(4));
put(double.class, new Integer(8));
put(long.class, new Integer(8));
}
};
class MemorySizes
public int getPrimitiveFieldSize(Class clazz) {
return ((Integer) primitiveSizes.get(clazz)).intValue();
}
public int getPrimitiveArrayElementSize(Class clazz) {
return getPrimitiveFieldSize(clazz);
}
public int getPointerSize() {
return 4; }
public int getClassSize() {
return 8;
}
class MemoryCounter && MemoryCounterExample
[java] Test: testString Object-Type: java.lang.String estimated: 64
[java] Test: IntegerToString Object-Type: java.lang.String estimated: 72
[java] Test: testHashMap Object-Type: java.util.HashMap estimated: 120
[java] Test: testHashMap Object-Type: [Ljava.lang.Byte; estimated: 5136
[java] Test: testHashMap Object-Type: java.util.HashMap estimated: 30776
[java] Test: testVector Object-Type: java.util.Vector estimated: 80
[java] Test: testObject Object-Type: java.lang.Object estimated: 8
[java] Test: testInteger Object-Type: java.lang.Integer estimated: 16
[java] Test: testCharArray Object-Type: [C estimated: 40
[java] Test: testByte Object-Type: java.lang.Byte estimated: 16
[java] Test: testThreeBytes Object-Type: s2.g0.MemoryCounterExample$ThreeBytes estimated: 16
[java] Test: testSixtyFourBooleans Object-Type: s2.g0.MemoryCounterExample$SixtyFourBooleans estimated: 72
[java] Test: testThousandBooleansObjects Object-Type: [Ljava.lang.Boolean; estimated: 20016
[java] Test: testThousandBytes Object-Type: [B estimated: 1016
[java] Test: testEmptyArrayList Object-Type: java.util.ArrayList estimated: 80
[java] Test: testFullArrayList Object-Type: java.util.ArrayList estimated: 120040
[java] Test: testFullLinkedList Object-Type: java.util.LinkedList estimated: 320048
[java] Test: testComplexClass Object-Type: s2.g0.MemoryCounterExample$ComplexClass estimated: 48
[java] Test: testBooleanArray Object-Type: [Z estimated: 27
[java] Test: testShortArray Object-Type: [S estimated: 38
[java] Test: testIntArray Object-Type: [I estimated: 60
[java] Test: testFloatArray Object-Type: [F estimated: 60
[java] Test: testLongArray Object-Type: [J estimated: 104
[java] Test: testDoubleArray Object-Type: [D estimated: 104
Laufzeitberechnung
public class Timer {
public static long time(Runnable r) {
long time = -System.currentTimeMillis();
r.run();
time += System.currentTimeMillis();
System.out.println("Took " + time + "ms");
return time;
}
}
MultArray
[java] Laufzeit MultArray
[java] Time Elapsed for [][4] [java] Took 12443ms
[java] Time Elapsed for [4][] [java] Took 3674ms
[java] Time Elapsed for [] [java] Took 599ms
StringAppendDiff
System.out.println("String += 10000 additions");
Timer.time(new Runnable() {
public void run() {
String s = "";
for (int i = 0; i < 10000; i++) {
s += i;
}
System.out.println("Length = " + s.length());
}
});
StringAppendDiff
System.out.println("StringBuffer 300 * 10000 additions " +
"initial size wrong");
Timer.time(new Runnable() {public void run() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < (300 * 10000); i++) {
sb.append(i);
}
String s = sb.toString();
System.out.println("Length = " + s.length());
}}); // StringBuffer initial size right analog mit
// new StringBuffer(19888890);
StringAppendDiff
[java] Laufzeit StringAppendDiff:
[java] String += 10000 additions
[java] Length = 38890
[java] Took 3945ms
[java] StringBuffer 300 * 10000 additions initial size wrong
[java] Length = 19888890
[java] Took 2831ms
[java] StringBuffer 300 * 10000 additions initial size right
[java] Length = 19888890
[java] Took 1656ms
Array v.s. Linked List
[java] Laufzeit ArrayList v.s. LinkedList:
[java] beginning java.util.ArrayList took 4913
[java] beginning java.util.LinkedList took 14
[java] middle java.util.ArrayList took 1249
[java] middle java.util.LinkedList took 45963
[java] end
java.util.ArrayList took 2648
[java] end
java.util.LinkedList took 4193
-> LinkedList eigentlich nur bei FIFO-List
Mögliche Aufgaben
JUnit test für MemoryCounter
Dokumentation bzw.
Zusammenfassung für
MemoryCounter
ByteCode - StringAppendDiff
Reflection in MemoryCounter
erklären.
- Wie sieht es eigentlich mit
verschiedenen JVM/Plattform
Kombinationen aus?
Herunterladen