Skip to content
William Zhang edited this page Mar 23, 2017 · 11 revisions

Perf

Remote debugging

Note: pass arguments to JVM is different for invoking java directly and using mvn.

$ java -Xdebug -Xrunjdwp:transport=dt_socket,address=60379,server=y,suspend=n -cp .:target/foo.jar -Dprop=props.my org.foo.Main -v 1

JMX and jconsole/jvisualvm etc.

$ mvn -Dmaven.test.jvmargs='-Xms16384m -Xmx16384m -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=60379 -Dcom.sun.management.jmxremote.rmi.port=60380 -Dcom.sun.management.jmxremote.local.only=false -Djava.rmi.server.hostname=10.x.y.z' -Dtest=org.foo.project.integrate.IntegrateFoo:testBar -DfailIfNoTests=false test

JDK

How to print the JVM settings?

$ java -XshowSettings:properties -version

hprof

http://docs.oracle.com/javase/7/docs/technotes/samples/hprof.html

jstack

http://flysnowxf.iteye.com/blog/1162691

  1. top or ps -ef to find the pid
  2. top -p <pid>, then shift+h to show threads, and find the interesting tid
  3. jstack <pid> | grep -A 10 <tid>

jstat, jmap, jhat, jdb, jps, jinfo etc.

http://ju.outofmemory.cn/entry/147496

http://www.cnblogs.com/yyyt/p/4307828.html

http://docs.oracle.com/javase/6/docs/technotes/tools/share/jstat.html

http://petermodzelewski.blogspot.com/2013/06/short-jhat-tutorial-diagnosing.html

Memory usage

$ jps -l

$ jmap -histo:live 20627

$ jmap -dump:file=dump.map 20627
$ jhat -port 7401 -J-mx4G dump.map

Memory usage and GC times/timestamp. Where 1000 means 1ms, 5 means 5 times.

$ jstat -gcutil 20627 1000 5

Eclipse

Install Check style plugin

When importing a Maven project, it may depends on check style plugin and report several errors like this: https://issues.apache.org/jira/browse/CALCITE-994. The solution is to place the attached file there to the top dir of the project and let Eclipse knows it (as described in the link).

The next problem is to install check style plugin. The automatically prompted steps may fail. And I have to follow the manual steps listed at the bottom of http://eclipse-cs.sourceforge.net/#!/install.

Disable code formatter

//
// @formatter:off
// Comments
// @formatter:on
//

Enable assertion

Windows -> Preferences -> Java -> Installed JREs -> Edit -> Default VM arguments:

-ea
-XX:+CreateMinidumpOnCrash

WinDBG (hide it here):

srv*D:\SymbolCache*https://msdl.microsoft.com/download/symbols

Java language

How to print the backtrace?

for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
    System.out.println(ste);
}

Why use `b & 0xFF` in bitwise operators?

Bitwise operators in Java is not the same as that in C/C++. Read this great article for details.

Also see long & 0xFFFFFFFF vs. long & 0xFFFFFFFFL.

log4j

  1. 1.x to 1.2 migrate link.
  2. 1.2 sample with Maven link.

jmap

http://stackoverflow.com/questions/5085889/l-array-notation-where-does-it-come-from/12505922#12505922

[Z = boolean [B = byte [S = short [I = int [J = long [F = float [D = double [C = char [L = any non-primitives(Object)

To get the main data-type, you need: [Object].getClass().getComponentType(); It will return null if the “object” is not an array. to determine if it is an array, just call: [Any Object].getClass().isArray() or Class.class.isArray();

CRC

http://pzemtsov.github.io/2015/11/21/crc32-intrinsic-java-or-c-with-jni.html

Ignore Errors/Warnings in files

<Right click a project> -> Build Path -> Configure Build Path -> Ignore optional compile problems: Yes

http://stackoverflow.com/questions/3736208/how-to-exclude-a-folder-that-is-producing-warnings-errors-in-an-eclipse-project