好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解JVM 运行时内存使用情况监控

java 语言, 开发者不能直接控制程序运行 内存 , 对象的创建都是由类加载器一步步解析, 执行与生成与内存区域中的; 并且jvm有自己的垃圾回收器对内存区域管理, 回收; 但是我们已经可以通过一些工具来在程序运行时查看对应的jvm内存使用情况, 帮助更好的分析与优化我们的代码;

注: 查看系统里java进程信息

?

1

2

3

4

// 查看当前机器上所有运行的java进程名称与pid(进程编号)

jps -l

// 显示指定的jvm进程所有的属性设置和配置参数

jinfo pid

1 . jmap : 内存占用情况查询 (位于]jdk_home/bin]目录下)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

// 查询某个pid进程对应的应用程序内存占用情况

jmap -heap pid

 

// 示例

jmap -heap 5940

attaching to process id 5940 , please wait...

debugger attached successfully.

server compiler detected.

jvm version is 25.92 -b14

 

using thread-local object allocation.

parallel gc with 4 thread(s)

 

heap configuration:

  minheapfreeratio   = 0

  maxheapfreeratio   = 100

  maxheapsize    = 734003200 ( 700 .0mb)

  newsize     = 44040192 ( 42 .0mb)

  maxnewsize    = 244318208 ( 233 .0mb)

  oldsize     = 88080384 ( 84 .0mb)

  newratio     = 2

  survivorratio   = 8

  metaspacesize   = 21807104 ( 20 .796875mb)

  compressedclassspacesize = 1073741824 ( 1024 .0mb)

  maxmetaspacesize   = 17592186044415 mb

  g1heapregionsize   = 0 ( 0 .0mb)

 

heap usage:

ps young generation

eden space:

  capacity = 32505856 ( 31 .0mb)

  used  = 13906760 ( 13 .262519836425781mb)

  free  = 18599096 ( 17 .73748016357422mb)

  42.782322052986395 % used

from space:

  capacity = 6291456 ( 6 .0mb)

  used  = 294912 ( 0 .28125mb)

  free  = 5996544 ( 5 .71875mb)

  4.6875 % used

to space:

  capacity = 7340032 ( 7 .0mb)

  used  = 0 ( 0 .0mb)

  free  = 7340032 ( 7 .0mb)

  0.0 % used

ps old generation

  capacity = 41943040 ( 40 .0mb)

  used  = 6127536 ( 5 .8436737060546875mb)

  free  = 35815504 ( 34 .15632629394531mb)

  14.609184265136719 % used

 

8535 interned strings occupying 710344 bytes.

2 . jstack : 进程所包含线程情况查询 (位于]jdk_home/bin]目录下)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

// 命令帮助

jstach -h

// 显示进程详情

jstack pid

 

// 示例

jstach 5940

full thread dump java hotspot(tm) 64 -bit server vm ( 25.92 -b14 mixed mode):

 

"rmi tcp connection(10)-10.2.13.162" # 32 daemon prio= 5 os_prio= 0 tid= 0x00000000179dc000 nid= 0x1f60 in object.wait() [ 0x000000001d7dd000 ]

  java.lang.thread.state: timed_waiting (on object monitor)

   at java.lang.object.wait( native method)

   at com.sun.jmx.remote.internal.arraynotificationbuffer.fetchnotifications(arraynotificationbuffer.java: 449 )

   - locked < 0x00000000d462ec18 > (a com.sun.jmx.remote.internal.arraynotificationbuffer)

   at com.sun.jmx.remote.internal.arraynotificationbuffer$sharebuffer.fetchnotifications(arraynotificationbuffer.java: 227 )

   at com.sun.jmx.remote.internal.servernotifforwarder.fetchnotifs(servernotifforwarder.java: 274 )

   at javax.management.remote.rmi.rmiconnectionimpl$ 4 .run(rmiconnectionimpl.java: 1270 )

   at javax.management.remote.rmi.rmiconnectionimpl$ 4 .run(rmiconnectionimpl.java: 1268 )

   at javax.management.remote.rmi.rmiconnectionimpl.fetchnotifications(rmiconnectionimpl.java: 1274 )

   at sun.reflect.generatedmethodaccessor59.invoke(unknown source)

   at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java: 43 )

   at java.lang.reflect.method.invoke(method.java: 498 )

   at sun.rmi.server.unicastserverref.dispatch(unicastserverref.java: 324 )

 

..............

 

"gc task thread#1 (parallelgc)" os_prio= 0 tid= 0x0000000002d10000 nid= 0x27dc runnable

 

"gc task thread#2 (parallelgc)" os_prio= 0 tid= 0x0000000002d11800 nid= 0x2d84 runnable

 

"gc task thread#3 (parallelgc)" os_prio= 0 tid= 0x0000000002d13800 nid= 0x118 runnable

 

"vm periodic task thread" os_prio= 2 tid= 0x0000000015ccb000 nid= 0x2fd4 waiting on condition

 

jni global references: 239

3 . jstat: 可以实时监测系统资源占用与jvm运行情况 (位于]jdk_home/bin]目录下)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

// 命令语法结构:

usage: jstat -help|-options

   jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

 

// 参数解释:

options — 选项,我们一般使用 -gcutil 查看gc情况

vmid — vm的进程号,即当前运行的java进程号

interval– 间隔时间,单位为秒或者毫秒

count — 打印次数,如果缺省则打印无数次

 

s0 — heap上的 survivor space 0 区已使用空间的百分比

s1 — heap上的 survivor space 1 区已使用空间的百分比

e — heap上的 eden space 区已使用空间的百分比

o — heap上的 old space 区已使用空间的百分比

p — perm space 区已使用空间的百分比

ygc — 从应用程序启动到采样时发生 young gc 的次数

ygct– 从应用程序启动到采样时 young gc 所用的时间(单位秒)

fgc — 从应用程序启动到采样时发生 full gc 的次数

fgct– 从应用程序启动到采样时 full gc 所用的时间(单位秒)

gct — 从应用程序启动到采样时用于垃圾回收的总时间(单位秒)

 

//示例

jstat -options

  - class

  -compiler

  -gc

  -gccapacity

  -gccause

  -gcmetacapacity

  -gcnew

  -gcnewcapacity

  -gcold

  -gcoldcapacity

  -gcutil

  -printcompilation

 

jstat - class -t 5940

timestamp loaded bytes unloaded bytes  time

6188.4 3898 7178.4   40 58.3   1.78

 

jstat -gcutil 5940 1000 5

s0  s1  e  o  m  ccs ygc  ygct fgc fgct  gct

0.00 25.00 98.55 15.37 96.94 94.88   21 0.069   7 0.237 0.306

0.00 25.00 99.59 15.37 96.94 94.88   21 0.069   7 0.237 0.306

0.00 25.00 99.59 15.37 96.94 94.88   21 0.069   7 0.237 0.306

0.00 25.00 100.00 15.37 96.94 94.88   21 0.069   7 0.237 0.306

0.00 25.00 100.00 15.37 96.94 94.88   21 0.069   7 0.237 0.306

4 . jconsole 以gui的方式更直观化呈现jvm进程的实时情况, 比如内存占用, 线程执行情况等;
在jdk_home/bin目录下执行 jconsole.exe 打开图形化界面, 然后选择要检查的进程就可以查看所有相关jvm情况的信息了.

5 . jprofiler 一个付费的商业jvm分析 监控 工具, 可查看概况, 内存, gc活动, class 状况, 线程信息, cpu 占用情况, 内存对象信息, 还有数据库连接等, 是一个非常优秀的分析工具;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/HinstenyHisoka/article/details/54311722

查看更多关于详解JVM 运行时内存使用情况监控的详细内容...

  阅读:21次