Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
JVM Memory  2013.4.19 Xuefeng.Wu  Benewu(at)gmail.com
Agenda• JVM Memory Layout with flag and OOM• GC• Debug toolshttp://docs.oracle.com/javase/specs/ Run-Time Data Areashttp:/...
Memory
JVM memory overview
JVM Memory Parametes
Code
References and objects
Runtime Data Area
VM
http://www.2cto.com/kf/201108/99433.html
Java Object Heap• -Xmx,-Xms,-Xmn• Eden, Survivor Space• -XX:NewRatio =80%, -XX:Survivor• Old Generation --- Long live, Big...
Perm Space•   -XX:MaxPermSize=128m• Classes, method• String pools
Code Generation• Bytecode to native code• 10k calls• JVM will crash
Socket Buffer• Revieve ~37K• Send ~25k• Java.lang.StackOverflowError• Demo: SocketBuffer.java• Too many open files
Thread stack• -Xss• Each thread has a separate memory space• Default value dependence on OS• Demo: ThreadAllocation.java
Direct Memory Space• Map memory outside the java object heap• Java.nio.ByteBuffer.allocateDirect• -XX:MaxDirectMemorySize=...
JNI• JNI programs allocate memory
GC• Threads• GC information
GARBAGECOLLECTION
How does garbage collection work?
JVM roots
基本GC收集算法• 复制(copying)• 标记清除(mark-sweep)• 标记整理(mark-sweep-compact)
复制(copying)
标记清除(mark-sweep)
标记整理(mark-sweep-   compact)
GC收集器类型• 古老的串行收集器(Serial Collector)• 吞吐量优先的并行收集器(Throughput Collector)• 暂停时间优先的并发收集器(Concurrent Low Pause  Collector-CMS)•...
古老的串行收集器(Serial   Collector)
吞吐量优先的并行收集器(Throughput Collector)              -XX:+UseParallelGC
(Concurrent Low Pause   Collector-CMS)             -XX:+UseConcMarkSweepGC
Concurrent-Mark-Sweep/i-         CMS)
SUN JDK GC
JVM parameters in Javahttp://javarevisited.blogspot.com/2011/11/hotspot-jvm-options-java-examples.html
Tools
JVM Tools
jps(Java Virtual Machine     Process Status Tool)• 用来查看基于HotSpot JVM里面所有进程的具体状态, 包  括进程ID,进程启动的路径等等。• jps [ options ] [ ho...
jstack(Java Stack Trace)• jstack用于打印出给定的java进程ID或core file或远程调  试服务的Java堆栈信息,如果是在64位机器上,需要指  定选项"-J-d64“• jstack [-l] pid
jstat(Java Virtual Machine Statistics Monitoring Tool)• Jstat是JDK自带的一个轻量级小工具。利用JVM内建的指  令对Java应用程序的资源和性能进行实时的命令行的监  控,包括了对...
jmap(Java Memory Map)• 打印出某个java进程(使用pid)内存内的,所有‘对象’的  情况(如:产生那些对象,及其数量)。• jmap -J-d64 -heap pid• jmap -dump:format=b,file...
jhat(Java Heap Analyse To            ol)• jhat是一个Java堆复制浏览器。这个工具分析Java堆复  制文件(例如,由上面的"jmap -dump"所产生的)。  Jhat启动一个允许堆中的对象在w...
jvisualvm
BTrace
BTrace
Btrace script                Demo
JMX• JMX(Java Management Extensions,即Java管理扩  展)是Java平台上为应用程序、设备、系统等植入管理  功能的框架。
JMSMonitor  -Dcom.sun.management.jmxremote.port=3333 -  Dcom.sun.management.jmxremote.ssl=false -  Dcom.sun.management.jmx...
MBeans
Jvm memory
Upcoming SlideShare
Loading in …5
×

Jvm memory

1,254 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Jvm memory

  1. 1. JVM Memory 2013.4.19 Xuefeng.Wu Benewu(at)gmail.com
  2. 2. Agenda• JVM Memory Layout with flag and OOM• GC• Debug toolshttp://docs.oracle.com/javase/specs/ Run-Time Data Areashttp://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5
  3. 3. Memory
  4. 4. JVM memory overview
  5. 5. JVM Memory Parametes
  6. 6. Code
  7. 7. References and objects
  8. 8. Runtime Data Area
  9. 9. VM
  10. 10. http://www.2cto.com/kf/201108/99433.html
  11. 11. Java Object Heap• -Xmx,-Xms,-Xmn• Eden, Survivor Space• -XX:NewRatio =80%, -XX:Survivor• Old Generation --- Long live, Big Object• Demo:BigObject.java
  12. 12. Perm Space• -XX:MaxPermSize=128m• Classes, method• String pools
  13. 13. Code Generation• Bytecode to native code• 10k calls• JVM will crash
  14. 14. Socket Buffer• Revieve ~37K• Send ~25k• Java.lang.StackOverflowError• Demo: SocketBuffer.java• Too many open files
  15. 15. Thread stack• -Xss• Each thread has a separate memory space• Default value dependence on OS• Demo: ThreadAllocation.java
  16. 16. Direct Memory Space• Map memory outside the java object heap• Java.nio.ByteBuffer.allocateDirect• -XX:MaxDirectMemorySize=<value>
  17. 17. JNI• JNI programs allocate memory
  18. 18. GC• Threads• GC information
  19. 19. GARBAGECOLLECTION
  20. 20. How does garbage collection work?
  21. 21. JVM roots
  22. 22. 基本GC收集算法• 复制(copying)• 标记清除(mark-sweep)• 标记整理(mark-sweep-compact)
  23. 23. 复制(copying)
  24. 24. 标记清除(mark-sweep)
  25. 25. 标记整理(mark-sweep- compact)
  26. 26. GC收集器类型• 古老的串行收集器(Serial Collector)• 吞吐量优先的并行收集器(Throughput Collector)• 暂停时间优先的并发收集器(Concurrent Low Pause Collector-CMS)• 增量并发收集器(Incremental Concurrent-Mark- Sweep/i-CMS)
  27. 27. 古老的串行收集器(Serial Collector)
  28. 28. 吞吐量优先的并行收集器(Throughput Collector) -XX:+UseParallelGC
  29. 29. (Concurrent Low Pause Collector-CMS) -XX:+UseConcMarkSweepGC
  30. 30. Concurrent-Mark-Sweep/i- CMS)
  31. 31. SUN JDK GC
  32. 32. JVM parameters in Javahttp://javarevisited.blogspot.com/2011/11/hotspot-jvm-options-java-examples.html
  33. 33. Tools
  34. 34. JVM Tools
  35. 35. jps(Java Virtual Machine Process Status Tool)• 用来查看基于HotSpot JVM里面所有进程的具体状态, 包 括进程ID,进程启动的路径等等。• jps [ options ] [ hostid ]
  36. 36. jstack(Java Stack Trace)• jstack用于打印出给定的java进程ID或core file或远程调 试服务的Java堆栈信息,如果是在64位机器上,需要指 定选项"-J-d64“• jstack [-l] pid
  37. 37. jstat(Java Virtual Machine Statistics Monitoring Tool)• Jstat是JDK自带的一个轻量级小工具。利用JVM内建的指 令对Java应用程序的资源和性能进行实时的命令行的监 控,包括了对Heap size和垃圾回收状况的监控。• jstat [ generalOption | outputOptions vmid [interval [s|ms] [count]] ]
  38. 38. jmap(Java Memory Map)• 打印出某个java进程(使用pid)内存内的,所有‘对象’的 情况(如:产生那些对象,及其数量)。• jmap -J-d64 -heap pid• jmap -dump:format=b,file=test.bin 4939
  39. 39. jhat(Java Heap Analyse To ol)• jhat是一个Java堆复制浏览器。这个工具分析Java堆复 制文件(例如,由上面的"jmap -dump"所产生的)。 Jhat启动一个允许堆中的对象在web浏览器中进行分析 的web服务器。• jhat -J-mx768m -port <端口号:默认为7000> heap.dmp
  40. 40. jvisualvm
  41. 41. BTrace
  42. 42. BTrace
  43. 43. Btrace script Demo
  44. 44. JMX• JMX(Java Management Extensions,即Java管理扩 展)是Java平台上为应用程序、设备、系统等植入管理 功能的框架。
  45. 45. JMSMonitor -Dcom.sun.management.jmxremote.port=3333 - Dcom.sun.management.jmxremote.ssl=false - Dcom.sun.management.jmxremote.authenticate=false - Djava.rmi.server.hostname=10.112.37.92JMXServiceURL url = newJMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:3333/jmxrmi");JMXConnector jmxConnector = JMXConnectorFactory.connect(url);MBeanServerConnection serverConn =jmxConnector.getMBeanServerConnection();MBeanServerConnection serverConn =jmxConnector.getMBeanServerConnection(); Demo:JMSMonitor.javaPrintln(memoryMXBean.getHeapMemoryUsage().getUsed());
  46. 46. MBeans

×