<commit_before>---
layout: post
title: Java代码调试总结(待续)
subtitle: 远程java debug,VisualVM远程监控JVM
author: carm
date: 2019-04-02 21:14:22 +0800
header-img: img/home-bg.jpg
categories: java
tag:
  - jvm
  - 调试
---
# Java代码调试总结

### 远程Debug

java启动命令后面添加参数：

`-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005` 

 **suspend**可选值为： `y/n`

### 打印GC信息 ###
`-Xms20M -Xmx20M -Xmn10M -verbose:gc -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+PrintGCTimeStamps
-XX:+PrintGCDetails -Xloggc:/tmp/gc.log -XX:+PrintGCTimeStamps`


### Spark executor远程Debug
`spark-submit --conf spark.executor.extraJavaOptions=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=4009`


### Java堆溢出Dump
> -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdump


### 打印Log Debug日志
设置类的日志级别为debug
>log4j.logger.org.apache.spark.scheduler.DAGScheduler=DEBUG

输出相关日志到一个单独的文件中

> log4j.logger.org.apache.spark.scheduler.DAGScheduler=DEBUG,TTOUT
log4j.appender.TTOUT=org.apache.log4j.RollingFileAppender
log4j.appender.TTOUT.File=${log.dir}/DAGScheduler.log
log4j.appender.TTOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.TTOUT.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n


### VisualVM远程监控 ###
1、启动jstatd服务

    创建安全策略文件
    # mkdir /home/carm/security
    # vim jstatd-all.policy

    在jstatd-all.policy文件中写入下面内容：
    
    grant codebase "file:${java.home}/../lib/tools.jar" {   
        permission java.security.AllPermission;
    };

    启动jstatd
    jstatd -p 1099 -J-Djava.security.policy=/home/carm/security/jstatd-all.policy -J-Djava.rmi.server.hostname=10.3.71.103


2、 本地VisualVM配置
<commit_msg>Update 2019-04-02-Java代码调试总结.md<commit_after>---
layout: post
title: Java代码调试总结(待续)
subtitle: 远程java debug,VisualVM远程监控JVM
author: carm
date: 2019-04-02 21:14:22 +0800
header-img: img/home-bg.jpg
categories: java
tag:
  - jvm
  - 调试
---
# Java代码调试总结

### 远程Debug

java启动命令后面添加参数：

`-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005` 

 **suspend**可选值为： `y/n`

### 打印GC信息 ###
`-Xms20M -Xmx20M -Xmn10M -verbose:gc -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+PrintGCTimeStamps
-XX:+PrintGCDetails -Xloggc:/tmp/gc.log -XX:+PrintGCTimeStamps`


### Spark executor远程Debug
`spark-submit --conf spark.executor.extraJavaOptions=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=4009`


### Java堆溢出Dump
> -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdump


### 打印Log Debug日志
修改log4j.properties文件，设置类的日志级别为debug
>log4j.logger.org.apache.spark.scheduler.DAGScheduler=DEBUG

输出相关日志到一个单独的文件中

> log4j.logger.org.apache.spark.scheduler.DAGScheduler=DEBUG,TTOUT
log4j.appender.TTOUT=org.apache.log4j.RollingFileAppender
log4j.appender.TTOUT.File=${log.dir}/DAGScheduler.log
log4j.appender.TTOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.TTOUT.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n


### VisualVM远程监控 ###
1、启动jstatd服务

    创建安全策略文件
    # mkdir /home/carm/security
    # vim jstatd-all.policy

    在jstatd-all.policy文件中写入下面内容：
    
    grant codebase "file:${java.home}/../lib/tools.jar" {   
        permission java.security.AllPermission;
    };

    启动jstatd
    jstatd -p 1099 -J-Djava.security.policy=/home/carm/security/jstatd-all.policy -J-Djava.rmi.server.hostname=10.3.71.103


2、 本地VisualVM配置

{'fraction_non_alphanumeric': 0.14934471197805546, 'fraction_numerical': 0.0399268515696434, 'mean_word_length': 4.768014059753955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3389494', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1572, 'n_words': 163}