tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

30
Kees Jan Koster kjkoster@ java-monitor.com new Object(); as quick as updating a pointer…

Upload: techtalks

Post on 14-Apr-2017

183 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

new Object();as quick as updating a pointer…

Page 2: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

pointer += bytesToAllocate;

Page 3: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

pointer += bytesToAllocate;

heap

Page 4: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

heap

Page 5: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

heap

Page 6: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

heap

… significant pause …

option 1: compacting collector

Page 7: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

heap

1k2k4k

option 2: non-compacting collector

Page 8: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

old generation

1k2k4k

eden

generational garbage collection

Page 9: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

old generationeden survivor spaces

scavenger minor gc

mark-sweep full gc

Page 11: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

throughput garbage collector (-XX:+UseParallelGC -XX:+UseParallelOldGC)

* compacting (no unexpected long pauses)* relatively low CPU overhead* uses multiple cores to collect

CPU bound (batch) application?

yesserial garbage collector (-XX:+UseSerialGC)

* short pauses since the heap is tiny* compacting (no unexpected long pauses)* low CPU overhead

heap <= 128 MBand/or

single CPU?

G1 collector (-XX:+UseG1GC)

* concurrent GC, normally low pauses* non-compacting (may cause long pauses)* high CPU overhead

CMS collector (-XX:+UseConcMarkSweepGC -XX:+UseParNewGC)

* concurrent GC, normally low pauses* non-compacting (may cause long pauses)* high CPU overhead

heap <= 4GB

no

no

no

yes

yes

objective: - pick the most likely GC candidate

pick the right heap size for your application

Page 12: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

throughput garbage collector (-XX:+UseParallelGC -XX:+UseParallelOldGC)

* compacting (no unexpected long pauses)* relatively low CPU overhead* uses multiple cores to collect

CPU bound (batch) application?

yesserial garbage collector (-XX:+UseSerialGC)

* short pauses since the heap is tiny* compacting (no unexpected long pauses)* low CPU overhead

heap <= 128 MBand/or

single CPU?

G1 collector (-XX:+UseG1GC)

* concurrent GC, normally low pauses* non-compacting (may cause long pauses)* high CPU overhead

CMS collector (-XX:+UseConcMarkSweepGC -XX:+UseParNewGC)

* concurrent GC, normally low pauses* non-compacting (may cause long pauses)* high CPU overhead

heap <= 4GB

no

no

no

yes

yes

objective: - pick the most likely GC candidate

pick the right heap size for your application

Page 13: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

throughput garbage collector (-XX:+UseParallelGC -XX:+UseParallelOldGC)

* compacting (no unexpected long pauses)* relatively low CPU overhead* uses multiple cores to collect

CPU bound (batch) application?

yesserial garbage collector (-XX:+UseSerialGC)

* short pauses since the heap is tiny* compacting (no unexpected long pauses)* low CPU overhead

heap <= 128 MBand/or

single CPU?

G1 collector (-XX:+UseG1GC)

* concurrent GC, normally low pauses* non-compacting (may cause long pauses)* high CPU overhead

CMS collector (-XX:+UseConcMarkSweepGC -XX:+UseParNewGC)

* concurrent GC, normally low pauses* non-compacting (may cause long pauses)* high CPU overhead

heap <= 4GB

no

no

no

yes

yes

objective: - pick the most likely GC candidate

pick the right heap size for your application

Page 14: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

old generationeden

Page 15: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

old generationeden

synchronized (pointer) {pointer += bytesToAllocate;

}

Page 16: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

CPU CPU CPU

main memory acces time ~200 clock cycles

cache ~15 clks cache ~15 clks cache ~15 clks

http://gotocon.com/dl/jaoo-aarhus-2009/slides/BrianGoetz_TheConcurrencyRevolutionTheHardwareStory.pdf

Page 17: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

old generationeden

thread-local allocation buffer TLAB

Page 18: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

byte array processing vs streaming

Page 19: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

old generationeden

humongous allocation

Page 20: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

G1 humongous allocation

Page 21: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

old generationeden

promotion-local allocation buffer PLAB

Page 22: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

http://www.javaspecialists.eu/archive/Issue179.html

private static long test() {long total = 0;for (int i = 0; i < 10000; i++) {

CalculatorVarArgs calc = new CalculatorVarArgs();total += calc.add(i, i / 2);

}return total;

}

Page 23: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

https://blog.codecentric.de/en/2014/08/string-deduplication-new-feature-java-8-update-20-2/

-XX:+UseG1GC -XX:+UseStringDeduplication

"foo";"bar";"foo and bar";"bar";"bar";"foo";

"bar";"foo and bar";

"foo";

Page 24: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

OS memory

JVM

Page 25: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

OS memory

JVM

swap

nano-seconds milli-seconds

Page 26: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

OS memory

JVM

swap

Page 27: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

host memory

guest VM guest

Page 28: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

host memory swap

nano-seconds milli-seconds

guest VM guest guest

Page 29: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

OS memory swap

guest guest guestguest

Page 30: Tech talks annual 2015 kees jan koster_how to allocate an object in 10 nanoseconds

Kees Jan Koster [email protected]

pointer += bytesToAllocate;

Kees Jan Koster [email protected]

+31651838192