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

Post on 14-Apr-2017

183 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Kees Jan Koster kjkoster@java-monitor.com

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

Kees Jan Koster kjkoster@java-monitor.com

pointer += bytesToAllocate;

Kees Jan Koster kjkoster@java-monitor.com

pointer += bytesToAllocate;

heap

Kees Jan Koster kjkoster@java-monitor.com

heap

Kees Jan Koster kjkoster@java-monitor.com

heap

Kees Jan Koster kjkoster@java-monitor.com

heap

… significant pause …

option 1: compacting collector

Kees Jan Koster kjkoster@java-monitor.com

heap

1k2k4k

option 2: non-compacting collector

Kees Jan Koster kjkoster@java-monitor.com

old generation

1k2k4k

eden

generational garbage collection

Kees Jan Koster kjkoster@java-monitor.com

old generationeden survivor spaces

scavenger minor gc

mark-sweep full gc

Kees Jan Koster kjkoster@java-monitor.com

Kees Jan Koster kjkoster@java-monitor.com

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

Kees Jan Koster kjkoster@java-monitor.com

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

Kees Jan Koster kjkoster@java-monitor.com

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

Kees Jan Koster kjkoster@java-monitor.com

old generationeden

Kees Jan Koster kjkoster@java-monitor.com

old generationeden

synchronized (pointer) {pointer += bytesToAllocate;

}

Kees Jan Koster kjkoster@java-monitor.com

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

Kees Jan Koster kjkoster@java-monitor.com

old generationeden

thread-local allocation buffer TLAB

Kees Jan Koster kjkoster@java-monitor.com

byte array processing vs streaming

Kees Jan Koster kjkoster@java-monitor.com

old generationeden

humongous allocation

Kees Jan Koster kjkoster@java-monitor.com

G1 humongous allocation

Kees Jan Koster kjkoster@java-monitor.com

old generationeden

promotion-local allocation buffer PLAB

Kees Jan Koster kjkoster@java-monitor.com

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;

}

Kees Jan Koster kjkoster@java-monitor.com

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";

Kees Jan Koster kjkoster@java-monitor.com

OS memory

JVM

Kees Jan Koster kjkoster@java-monitor.com

OS memory

JVM

swap

nano-seconds milli-seconds

Kees Jan Koster kjkoster@java-monitor.com

OS memory

JVM

swap

Kees Jan Koster kjkoster@java-monitor.com

host memory

guest VM guest

Kees Jan Koster kjkoster@java-monitor.com

host memory swap

nano-seconds milli-seconds

guest VM guest guest

Kees Jan Koster kjkoster@java-monitor.com

OS memory swap

guest guest guestguest

Kees Jan Koster kjkoster@java-monitor.com

pointer += bytesToAllocate;

Kees Jan Koster kjkoster@gmail.com

+31651838192

top related