Вы находитесь на странице: 1из 72

:

Ekaterina.Pavlova@sun.com
Sun Microsystems


,
?
?

?
?
...
-
?
2

HotSpot JVM
JIT-

"out-of-box"

HotSpot JVM
Runtime
> class loading, ...



JIT
> client
> server

JVM
, JVM 0
, 0
- 0
,
,

HotSpot - JVM?

The perfect is the enemy of the good


6

HotSpot JVM
JIT-

"out-of-box"

JIT

Escape-


32bit 64bit jvm?


client 32bit
> ,

(, )
> ,
>

server 32bit, 64bit


> ,

( java , ...)
> ,

> ,
=>



>= 2Gb
# 2cpu >= 2

sparc 32-bit Solaris


Solaris
i586
Linux
Microsoft Windows
sparc64-bit
Solaris
Solaris
amd64
Linux
Microsoft Windows


client
server
client
server
client
server
client
client
server
server
server
server
server
server
server
server

10

?
# jinfo JAVA_PID
Java System Properties:
...
java.vm.version = 14.0-b05
java.vm.name = Java HotSpot(TM) Client VM
...
os.name = Linux
os.arch = i386
os.version = 2.6.8-24-smp
...
java.specification.name = Java Platform API Specification
java.class.version = 51.0
java.vm.specification.version = 1.0
sun.arch.data.model = 32
java.specification.vendor = Sun Microsystems Inc.
java.vm.info = mixed mode
java.version = 1.7.0-ea
...

11

Escape- (1)


class Escape1 {
Integer val; Escape1 next;
Escape1(Integer val) { this.val = val }
void example() {
Integer i1 = new Integer(1);
Integer i2 = new Integer(2);
Escape e1 = new Escape1( i1 );
Escape e2 = new Escape1( i2 );
e1.next = e2;
next = e2;
} }

O O M, :
>
>
>
>
>

M
Thread


,
, O
12

Escape- (2)
:
>
>
> , ,

(heap)

class Escape2 {
int fld1, fld2;
Escape2(int v1, int v2) { fld1 = v1; fld2 = v2; }
static void bigMethod() {
// , ...
}

=>

static void example(int v1, int v2) {


Escape2 e1 = new Escape2(v1, 10);
Escape2 e2 = new Escape2(v2, 5 - v1);
bigMethod();
return e1.fld1 + e2.fld2;
}

v1 + (5 v1) = 5
13

Java (1)
/

(contended). ..
>
>

>

CAS -
> ,


14

Java (2)
CAS

> (biased)
>
JVM


> -XX:+UseBiasedLocking

/ adaptive spinning -
> (spin)
while (lockStillInUse);

> (block)

15


(1)
2xP4
2xAMD
4xAMD
2xUS-III
1xUS-T1

70
60
50
40
30
20
10
0
-10
-20

DB

Jack

Javac

Jess

MTRT

JVM98

Scimark

JBB2000

JBB2005

Volano

16


(2)
2xP4
2xAMD
4xAMD
2xUS-III
1xUS-T1

300
250
200
150
100
50
0

Scimark2 Monte Carlo Sub-benchmark

17

Java (3)
/ lock elision
>
public String getStoogeNames() {
Vector v = new Vector();
v.add("One");
v.add("Two");
return v.toString();
}

> -XX:+DoEscapeAnalysis

/ lock coarsening
> synchronized
public void addStooges(Vector v) {
v.add("One");
v.add("Two");
}

> -XX:+EliminateLocks
18




>
> synchronized ,

> DTrace HotSpot monitor-contended-*


> JVMStat
jstat -J-Djstat.showUnsupported=true -snap <pid> | grep _sync_

19

arraycopy()
java.lang.System.arraycopy()
JDK
inline
HotSpot

>

20






>

>

-server -XX:+TieredCompilation
jdk7, 32bit jvm
21

64bit JVM?

>
> java heap > 4Gb

64bit (-d64) 32bit (-d32) jvm


> 64bit
> 64-


> D-cache

64bit 32bit jvm


> 2
> , 64-

C : -XX:+UseCompressedOops
32bit 64bit jvm
32Gb

22


HotSpot JVM
JIT-

"out-of-box"

23

GC


>
>
>
>
>
>
>
>



NUMA







24

(GC)

> roots (

, ..)


,
> Stop-the-world


>
> : copying, mark-sweep, mark-compact, ...
> : linear, free lists, ...

25



> (generations)
>


>
> M



(write barrier)

26


GC

>
> GC (

GC
>

27

Hotspot VM
GC

GC

Default GC

mark-sweep compact

Concurrent
Mark-Sweep

-XX:+UseConcMarkSweepGC
mark-sweep

Incremental
Concurrent
Mark-Sweep


marksweep

-XX:+UseConcMarkSweepGC
XX:+CMSIncrementalMode

Parallel
Scavenger

mark-sweep-compact

-XX:+UseParallelGC
-server

Parallel
Scavenger
Parallel Old

marksweep-compact

-XX:+UseParallelOldGC

Garbage-First
(G1)

-XX:+UseSerialGC
-client

-XX:+UseG1GC

JDK7

28

/Serial GC
- GC

Mark-Sweep Compact


(<200mb)
stop-the-world =>

29

/Concurrent Mark-Sweep GC
stop-the-world
, ,
-

30

GC

> ParallelGC

JIT (-server)
> SerialGC

(-Xmx)
> Serial GC 64Mb
> Parallel GC
min { RAM, 1Gb}
min { RAM, 32Gb}

31

?
# jmap JAVA_PID
Attaching to process ID 12345, please wait...
Debugger attached successfully.
Client compiler detected.
using thread-local object allocation.
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio
MaxHeapFreeRatio
MaxHeapSize
NewSize
MaxNewSize
OldSize
NewRatio
SurvivorRatio
PermSize
MaxPermSize
...

=
=
=
=
=
=
=
=
=
=

40
70
134217728 (128.0MB)
655360 (0.625MB)
4294901760 (4095.9375MB)
1441792 (1.375MB)
12
8
12582912 (12.0MB)
67108864 (64.0MB)

32

Netbeans ...
# jmap -heap NETBEANS_PID
Parallel GC with 2 thread(s)
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize
= 134217728 (128.0MB)
NewSize
= 0.625MB, MaxNewSize = 4095.9375MB
OldSize
= 1441792 (1.375MB)
PermSize
= 12.0MB, MaxPermSize = 64.0MB
Heap Usage:
PS Young Generation
Eden Space:
capacity = 7798784 (7.4375MB)
used = 4760384 (4.53985595703125MB)
free = 3038400 (2.89764404296875MB)
61.040080094537814% used
PS Old Generation
capacity = 30998528 (29.5625MB)
used = 10604488 (10.113227844238281MB)
free = 20394040 (19.44927215576172MB)
34.20965021306818% used
PS Perm Generation
capacity = 26083328 (24.875MB)
used = 25991728 (24.787643432617188MB)
free = 91600 (0.0873565673828125MB)
99.64881781956659% used
...

33

?
# jmap -histo NETBEANS_PID

...
Object Histogram:
Size Count Class description
------------------------------------------------------6371024 54861 * ConstMethodKlass
4834144 54861 * MethodKlass
3589960 44040 char[]
3519960 88375 * SymbolKlass
3470152 18522 int[]
3104776 4772
* ConstantPoolKlass
2160944 4772
* InstanceKlassKlass
2005360 4467
* ConstantPoolCacheKlass
1926008 5607
byte[]
1084200 45175 java.lang.String
630864 8661
java.lang.Object[]
615168 25632 java.util.HashMap$Entry
549208 8004
short[]
522120 6749
java.util.HashMap$Entry[]
465768 19407 java.awt.Rectangle
451880 5135
java.lang.Class
376776 7911
java.lang.Object[]
350552 1511
org.netbeans.editor.DrawEngine$DrawInfo
145152 3024
org.netbeans.editor.DrawEngineLineView
...
34

- PermGen?
:

Exception in thread X java.lang.OutOfMemoryError: PermGen space

# jmap -permstat NETBEANS_PID


...
finding class loader instances ... done.
computing per loader stat ........done.
Computing liveness...........................done.
class_loader classes bytes parent_loader alive? type
<bootstrap> 1189 2727472 null
live <internal>
0xee431d58 1
1392
0xee22b3d0
dead
sun/reflect/DelegatingClassLoader@0xd4042360
0xee3fbd08 1
1400
0xee227418
dead
sun/reflect/DelegatingClassLoader@0xd4042360
...
total = 65 2303 5900688 N/A alive=11, dead=54 N/A
35

-verbose:gc
-XX:+PrintGC
-XX:+PrintGCDetails
-XX:+PrintHeapAtGC
-XX:+PrintGCTimeStamps
jconsole
jvisualVM

36

GC


>
>
>
>
>
>
>
>



NUMA







37

GC?

>

GC


> GC


> , GC
> C GC / GC

38

(1)

>
>
>
>

10


C/C++


>


>

> ( GC)

39

(2)
(escape analysis)
>
> (lock elision)


>


>

40



>

> ()



> GC


> ,

41

NUMA (1)

>


> SPARC, Opteron, Intel

NUMA-
>


> ,

SPECjbb2005:

+40% 8-socket Opteron (8 ) SunFire x4600


+300% 72-socket (18 ) SunFire E25K
42

NUMA (2)
: -XX:+UseNUMA
> JDK6u2, Solaris 10
> Parallel Scavenger
> Linux

- (producer-consumer)
,

43


GC ,

> -
> : java.util.HashMap.table[] -> java.util.HashMap.Entry ->

key, value

table[]

Entry

key

value

Entry

key

value

>

Parallel Scavenger G1

44


GC
> GC
>
>


>

ArrayList

JDK!

>

45



> JIT-

void foo() {
int[] array = new int[1024];
populate(array);
print(array); //
// array foo()
array = null; // !
// array
//
...
}

46

(1)


> GC


>

>


> GC /
>

47

(2)

> ,
> ,

( ..).
(thread pools)

> JDK,

48

GC (1)
GC!
> GC ,

..
> System.gc()


>

HotSpot
> System.gc() stop-the-world GC
> -XX:+DisableExplicitGC

System.gc()

49

GC (2)
GC
> , GC
> System.gc() GC!

HotSpot ( Concurrent Mark Sweep)


> -XX:+ExplicitGCInvokesConcurrent


> , System.gc()

50

(1)

:
ArrayList<String> list = new ArrayList<String>();
list.ensureCapacity(1024);

:
ArrayList<String> list = new ArrayList<String>(1024);

51

(2)

ArrayList<String> list =
new ArrayList<String>(1024);

1,000,000 list?
>

>
>
>

52

GC


>
>
>
>
>
>
>
>



NUMA







53




finalize()

54

/

>
> VM


>
> 1-


> 2-
finalize()
finalize()
>

55




> GC ,

> GC ,
>


>

56

GC


>
>
>
>
>
>
>
>



NUMA







57


GC
,
!

> , ,
>

- ( )

58


Swing, AWT ..
{

ImageReader reader = new ImageReader();


cancelButton.addActionListener(reader);
Image image = reader.readImage(inputFile);
// reader
// cancelButton

ImageReader reader = new ImageReader();


cancelButton.addActionListener(reader);
Image image = reader.readImage(inputFile);
cancelButton.removeActionListener(reader);

59


(1)
try {
ImageReader reader = new ImageReader();
cancelButton.addActionListener(reader);
Image image = reader.readImage(inputFile);
cancelButton.removeActionListener(reader);
} catch (IOException e) {
// readImage()
// reader
// cancelButton
}

60


(2)
finally:
ImageReader reader = new ImageReader();
cancelButton.addActionListener(reader);
try {
Image image = reader.readImage(inputFile);
} catch (IOException e) {
...
} finally {
cancelButton.removeActionListener(reader);
}

61

(1)
:
>
>
class ImageManager {
private Map<Image,File> map =
new HashMap<Image,File>();
public void add(Image image, File file) { ... }
public void remove(Image image) { ... }
public File get(Image image) { ... }
}

62

(2)
remove(image)?
>
>

:
> image

, WeakHashMap
private Map<Image,File> map =
new WeakHashMap<Image,File>();

63


: jconsole, jstat, visual
gc, VisualVM ..

> jmap
> -XX:+PrintClassHistogram
Ctrl-Break


> jhat

( VisualVM)

64

HotSpot JVM
JIT-

"out-of-box"

65

"out-of-box"
jdk6 :
> SPECjbb2005 Java 6
40% Intel Core
30% AMD Opteron.
> SciMark Java 6
40%
> VolanoMark Java 6
Java 5 20%
> http://blogs.sun.com/vmrobot/entry/_jvm_6_0

JavaSE 6 performance releases


> Java SE

> http://java.sun.com/javase/technologies/performance.jsp

66

Normalized to IBM SDK 5.0 32-bit Linux

SPECjbb2005
140
120
100

114.89
100.43

100
89.68

88.8

80

71.2

60

54.98
J2SE 5.0_08
Java SE 6 RC1
IBM 5.0 SR2
BEA JRockit 5.0_06
R26.4

40
20
0

53.83

32-bit JVM

64-bit JVM
67

SciMark2
Normalized to IBM SDK 5.0 32-bit Linux

120
100

112.79
100

105.26

104.9

60

59.57

56.84
J2SE 5.0_08
Java SE 6 RC1
IBM 5.0 SR2
BEA JRockit 5.0_06
R26.4

40
20
0

79.09

77.07

80

32-bit JVM

64-bit JVM
68

Normalized to IBM SDK 5.0 32-bit Linux

Volano 2.5.0.9
119.97

120
100

113.02

110.71
100

96.06

112.02

96.69
88.93

80
60
J2SE 5.0_08
Java SE 6 RC1
IBM 5.0 SR2
BEA JRockit 5.0_06
R26.4

40
20
0

32-bit JVM

64-bit JVM
69

(1)

> http://java.sun.com/javase/technologies/performance.jsp
> http://java.sun.com/performance/reference/whitepapers/6_performance.html


> http://java.sun.com/j2se/reference/whitepapers/memorymanagement_whitepape

r.pdf

> Finalization, Threads, and the Java Technology Memory Model

http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-328
1.html

> Memory-retention due to finalization article


http://www.devx.com/Java/Article/30192

OpenJDK
> http://www.openjdk.org

70

(2)

> Monitoring and Management in 6.0

http://java.sun.com/developer/technicalArticles/J2SE/monitoring/

> Troubleshooting guide

http://java.sun.com/javase/6/webnotes/trouble/

> Jconsole

http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html

> VisualVM

https://visualvm.dev.java.net


> http://developers.sun.ru
> http://blogs.sun.com/vmrobot

:
, TechDays 2008,
http://developers.sun.ru/techdays2008
71

Ekaterina.Pavlova@sun.com
Sun Microsystems