29
Java Memory Problem Cases Solution bluedavy 2012-03

Java memory problem cases solutions

Embed Size (px)

DESCRIPTION

share the common java memory problem cases solutions,including: 1. java.lang.OutOfMemoryError 2. full gc frequently 3. cms gc error: promotion failed or concurrent mode failure

Citation preview

Page 1: Java memory problem cases solutions

Java Memory Problem Cases Solution

bluedavy 2012-03

Page 2: Java memory problem cases solutions

Basic Concepts

Java Runtime Data Area

PC

Local Vars

Operator

Stack Frame

Method Stack Heap

Native method Stack

Method Area

-Xss

-XX:PermSize –XX:MaxPermSize

-Xms -Xmx

Page 3: Java memory problem cases solutions

Basic Concepts

Oracle JDK Java Heap

Eden S0 S1 Old Generation

New Generation

-XX:SurvivorRatio

-Xmn

Page 4: Java memory problem cases solutions

Basic Concepts

Oracle JDK startup options for memory manage -Xms –Xmx –Xmn –XX:PermSize –XX:MaxPermSize -XX:SurvivorRatio -XX:+UseParallelGC || -XX:+UseParallelOldGC▪ -XX:MaxTenuringThreshold -XX:ParallelGCThreads▪ -XX:-UseAdaptiveSizePolicy

-XX:+UseConcMarkSweepGC▪ -XX:CMSInitiatingOccupancyFraction -XX:

+UseCMSInitiatingOccupancyOnly -XX:+DisableExplicitGC –XX:

+ExplicitGCInvokesConcurrent

Page 5: Java memory problem cases solutions

Basic Concepts

Startup options or tools for memory problem solution -XX:+PrintGCDateStamps –XX:+PrintGCDetails -Xloggc -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpBeforeFullGC -XX:+PrintFlagsFinal( JDK 6u21+) for CMS GC▪ -XX:+PrintPromotionFailure -XX:+CMSDumpAtPromotionFailure

jinfo jstat jmap MAT btrace google perf-tools

Page 6: Java memory problem cases solutions

Memory problems maybe occured

OOM java.lang.OutOfMemoryError: {$reason}

Full GC Frequently CMS GC

promotion failed concurrent mode failure

Page 7: Java memory problem cases solutions

OOM Case I

Page 8: Java memory problem cases solutions

OOM Case II

Page 9: Java memory problem cases solutions

OOM Case III

-Xms4g –Xmx4g –Xmn2560m

Page 10: Java memory problem cases solutions

OOM Case IV

Page 11: Java memory problem cases solutions

OOM Case V

Page 12: Java memory problem cases solutions

OOM Case V

Then to find who use Deflater.init

Page 13: Java memory problem cases solutions

OOM

java.lang.OutOfMemoryError: {$reason} GC overhead limit exceeded Java Heap Space Unable to create new native thread PermGen Space Direct buffer memory request {} bytes for {}. Out of swap

space?

Page 14: Java memory problem cases solutions

OOM will cause

GC overhead limit exceeded Java Heap Space

if code didn’t catch OutOfMemoryError,thread will exit;

slow response or no response at all▪ because gc did frequently

Out of Swap Java process crash

Page 15: Java memory problem cases solutions

OOM Solution

GC overhead limit exceededJava Heap Space monitor app log & gc log heap dump or jmap –histo|-dump when

oom mat analyze heap dump app developer or btrace to find where

cause

Page 16: Java memory problem cases solutions

OOM Solution

Out of swap crash log google-perftools then btrace to find where cause

ps: maybe u can try delete -XX:+DisableExplicitGC if exist,reason can be found in this link。

Page 17: Java memory problem cases solutions

write friendly code to OOM

use ThreadLocal carefully; limit Collection/StringBuilder etc.

size; limit batch ops size; limit the data size return by

database; avoid infinite loop;

Page 18: Java memory problem cases solutions

Memory problems maybe occured

OOM java.lang.OutOfMemoryError: {$reason}

Full GC Frequently CMS GC

promotion failed concurrent mode failure

Page 19: Java memory problem cases solutions

Full GC Frequently Case

Most Cases because memory consumes too much

a special case a special frequently GC Case

Page 20: Java memory problem cases solutions

Full GC frequently will cause

slow response or no response at all

Page 21: Java memory problem cases solutions

Full GC frequently solution

According gc log to see if the reason is consuming too much memory if yes then add -XX:

+HeapDumpBeforeFullGC or write a shell file to dump memory when full gc;

if no then use pstack & source code to see why full gc executes;

Page 22: Java memory problem cases solutions

Memory problems maybe occured

OOM java.lang.OutOfMemoryError: {$reason}

Full GC Frequently CMS GC

promotion failed concurrent mode failure

Page 23: Java memory problem cases solutions

CMS GC

promotion failed Case I 363439.937: [Full GC 363439.938:

[ParNew (promotion failed): 523840K->523840K(523840K), 0.3404490 secs] 1690606K->1712399K(1834560K), 0.3412880 secs]

Taobao JDK help to find the reason▪ ==WARNNING==  allocating large array:

thread_id[0x00002aaac2d85800], thread_name[ajp-0.0.0.0-8009-48], array_size[782611408 bytes], array_length[195652847 elememts]

Page 24: Java memory problem cases solutions

CMS GC

promotion failed Case II 1768.432: [GC 1768.432: [ParNew (promotion

failed): 1572827K->1572827K(1572864K), 0.2845480 secs]

1972.208: [CMS: 10545160K->5853155K(14680064K), 8.5355320 secs] 12105682K->5853155K(16252928K), [CMS Perm : 20907K->20873K(34952K)], 212.3113910 secs] [Times: user=53.38 sys=22.45, real=212.28 secs]

because cms gc fragmention

Page 25: Java memory problem cases solutions

CMS GC

four cases promotion failed case III▪ because CMSInitiatingOccupancyFraction too

high... concurrent mode failure case I▪ because CMSInitiatingOccupancyFraction too

high... concurrent mode failure case II▪ because old gen is too small

concurrent mode failure case III▪ because permgen

Page 26: Java memory problem cases solutions

CMS GC problem will cause

CMS GC problem will cause serial full gc,so the app response time will be very slow...

Page 27: Java memory problem cases solutions

CMS GC problem solution According gc log to see if the reason is

because CMSInitiatingOccupancyFraction is too high,if yes then adjust the option;

if no,then promotion failed▪ if because memory ran out,then dump memory;▪ if because cms gc fragmention,currently we only

can do is execute jmap –histo:live at regular time; concurrent mode failure▪ the same as promotion failed...

Page 28: Java memory problem cases solutions

Memory problems maybe occured

OOM java.lang.OutOfMemoryError: {$reason}

Full GC频繁 CMS GC

promotion failed concurrent mode failure

Page 29: Java memory problem cases solutions

Maybe u’ll occur other problems

Young GC slow CMS GC Concurrent-Mark causes app

stop... ...