Thread and Heap dumps are useful for debugging any Java programme.
To get thread dump:
By using jstack command we can get thread dump of a Java process.
$ jstack PID
where PID is Java process id for which we want to get thread dump.
To get heap dump:
By using jmap command we can get heap dump of a Java process.
$ jmap -heap PID
where PID is Java process id for which we want to get heap dump.
To replace multiple occurrences of a character in a string use following regular expression in your java code:
String s = "helloooooo";
s = s.replaceAll("(.)\\1+","$1");
In above code input string helloooooo is converted as hello
To install Oracle Java in Ubuntu operating system run following commands:
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
After running above commands you can install any of following versions of Oracle Java with apt-get:
If you want to install oracle-java7-installer user following command:
$ sudo apt-get install oracle-java7-installer
There are 2 options specify memory/heap size in java:
- -Xms: It sets initial (minimum) heap size for JVM. This means that when you start your program the JVM will allocate this amount of memory instantly.
- -Xmx: It sets maximum memory size that the heap can reach for the JVM.
Export Heap size in Linux:
$ export JAVA_OPTS="-Xms256m -Xmx512m"
Above command will set minimum heap size as 256MB, and maximum heap size as 512MB.
Set Heap size for tomcat in Linux:
Add following line in TOMCAT_HOME/bin/setenv.sh file
After adding restart your tamcat. It will will set minimum heap size as 512MB, and maximum heap size as 2048MB for your tomcat.
List of java escape characters shown below:
- \n : Newline
- \r : Carriage return
- \t : Tab
- \\ : Backslash
\b : backspace
\f : formfeed
\' : Single Quote Character
\" : Double quote character
\\ : Backslash character