How to add SWAP space in Linux to avoid Tomcat, Mysql being killed by the Kernel

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

If you Tomcat, Mysql, JAVA programs got killed by the kernel for memory space issues, adding a SWAP space in your Virtual Machine or Physical Machine may help.

Make sure you have login as root:
To create a 2GB swap space:

dd if=/dev/zero of=/mnt/swap bs=1024 count=2048000

Make it a swap area:

/sbin/mkswap /mnt/swap

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Make it live:

/sbin/swapon /mnt/swap

add the following codes in


to make SWAP area consistence after reboot.

/mnt/swap swap swap defaults 0 0

Read More

How to increment a Map value in Java

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Reference to

The guy from stackoverflow has tested several methods:

  • the “ContainsKey” method that I presented in the question
  • the “TestForNull” method suggested by Aleksandar Dimitrov
  • the “AtomicLong” method suggested by Hank Gay
  • the “Trove” method suggested by jrudolph
  • the “MutableInt” method suggested by


Here’s what I did…

  1. created five classes that were identical except for the differences shown below. Each class had to perform an operation typical of the scenario I presented: opening a 10MB file and reading it in, then performing a frequency count of all the word tokens in the file. Since this took an average of only 3 seconds, I had it perform the frequency count (not the I/O) 10 times.
  2. timed the loop of 10 iterations but not the I/O operation and recorded the total time taken (in clock seconds) essentially using Ian Darwin’s method in the Java Cookbook.
  3. performed all five tests in series, and then did this another three times.
  4. averaged the four results for each method.


I’ll present the results first and the code below for those who are interested.

The ContainsKey method was, as expected, the slowest, so I’ll give the speed of each method in comparison to the speed of that method.

  • ContainsKey: 30.654 seconds (baseline)
  • AtomicLong: 29.780 seconds (1.03 times as fast)
  • TestForNull: 28.804 seconds (1.06 times as fast)
  • Trove: 26.313 seconds (1.16 times as fast)
  • MutableInt: 25.747 seconds (1.19 times as fast)


It would appear that only the MutableInt method and the Trove method are significantly faster, in that only they give a performance boost of more than 10%. However, if threading is an issue, AtomicLong might be more attractive than the others (I’m not really sure). I also ran TestForNull with final variables, but the difference was negligible.

Note that I haven’t profiled memory usage in the different scenarios. I’d be happy to hear from anybody who has good insights into how the MutableInt and Trove methods would be likely to affect memory usage.

Personally, I find the MutableInt method the most attractive, since it doesn’t require loading any third-party classes. So unless I discover problems with it, that’s the way I’m most likely to go.

The code

Here is the crucial code from each method.


<code>import java.util.HashMap; import java.util.Map; ... Map&lt;String, Integer&gt; freq = new HashMap&lt;String, Integer&gt;(); ... int count = freq.containsKey(word) ? freq.get(word) : 0; freq.put(word, count + 1);</code>


<code>import java.util.HashMap; import java.util.Map; ... Map&lt;String, Integer&gt; freq = new HashMap&lt;String, Integer&gt;(); ... Integer count = freq.get(word); if (count == null) { freq.put(word, 1); } else { freq.put(word, count + 1); }</code>


<code>import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicLong; ... final ConcurrentMap&lt;String, AtomicLong&gt; map = new ConcurrentHashMap&lt;String, AtomicLong&gt;(); ... map.putIfAbsent(word, new AtomicLong(0)); map.get(word).incrementAndGet();</code>


<code>import gnu.trove.TObjectIntHashMap; ... TObjectIntHashMap&lt;String&gt; freq = new TObjectIntHashMap&lt;String&gt;(); ... freq.adjustOrPutValue(word, 1, 1);</code>


<code>import java.util.HashMap; import java.util.Map; ... class MutableInt { int value = 1; // note that we start at 1 since we're counting public void increment () { ++value; } public int get () { return value; } } ... Map&lt;String, MutableInt&gt; freq = new HashMap&lt;String, MutableInt&gt;(); ... MutableInt count = freq.get(word); if (count == null) { freq.put(word, new MutableInt()); } else { count.increment(); }</code>

Read More

Java Servlet International Character Encoding UTF-8 with Hibernate and MySQL 中文显示

Add following script to your hibernate.cfg.xml file. “useUnicode=true&characterEncoding=utf-8”

 <property name="hibernate.connection.url">jdbc:mysql://localhost/yourschema?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8</property>

如果你使用Java Servlet, Mysql和Hibernate, 为了正常显示及存储中文, 可以加入以上脚本

如果显示仍然有问题, 可以尝试使用:

String str="中文测试"; new String(str.getBytes("iso-8859-1"), "utf-8"); 

Read More

Java – Date, Time, SimpleDateFormat, String convert to Date

Simple DateFormat format codes:

To specify the time format use a time pattern string. In this pattern, all ASCII letters are reserved as pattern letters, which are defined as the following:

Character Description Example
G Era designator AD
y Year in four digits 2001
M Month in year July or 07
d Day in month 10
h Hour in A.M./P.M. (1~12) 12
H Hour in day (0~23) 22
m Minute in hour 30
s Second in minute 55
S Millisecond 234
E Day in week Tuesday
D Day in year 360
F Day of week in month 2 (second Wed. in July)
w Week in year 40
W Week in month 1
a A.M./P.M. marker PM
k Hour in day (1~24) 24
K Hour in A.M./P.M. (0~11) 10
z Time zone Eastern Standard Time
Escape for text Delimiter
Single quote `

Date and Time Conversion Characters:

Character Description Example
c Complete date and time Mon May 04 09:51:52 CDT 2009
F ISO 8601 date 2004-02-09
D U.S. formatted date (month/day/year) 02/09/2004
T 24-hour time 18:05:19
r 12-hour time 06:05:19 pm
R 24-hour time, no seconds 18:05
Y Four-digit year (with leading zeroes) 2004
y Last two digits of the year (with leading zeroes) 04
C First two digits of the year (with leading zeroes) 20
B Full month name February
b Abbreviated month name Feb
n Two-digit month (with leading zeroes) 02
d Two-digit day (with leading zeroes) 03
e Two-digit day (without leading zeroes) 9
A Full weekday name Monday
a Abbreviated weekday name Mon
j Three-digit day of year (with leading zeroes) 069
H Two-digit hour (with leading zeroes), between 00 and 23 18
k Two-digit hour (without leading zeroes), between 0 and 23 18
I Two-digit hour (with leading zeroes), between 01 and 12 06
l Two-digit hour (without leading zeroes), between 1 and 12 6
M Two-digit minutes (with leading zeroes) 05
S Two-digit seconds (with leading zeroes) 19
L Three-digit milliseconds (with leading zeroes) 047
N Nine-digit nanoseconds (with leading zeroes) 047000000
P Uppercase morning or afternoon marker PM
p Lowercase morning or afternoon marker pm
z RFC 822 numeric offset from GMT -0800
Z Time zone PST
s Seconds since 1970-01-01 00:00:00 GMT 1078884319
Q Milliseconds since 1970-01-01 00:00:00 GMT 1078884319047


SimpleDateFormat millisecFormat = new SimpleDateFormat("hh:mm:ss.mmm dd/MM/yy");
Date date = millisecFormat.parse("16:15:00.000 12/08/2009");

Read More

Example: Java Post Json String using –Java Restful

Java Post Request Json String to a URL using

Java Restful API, Java Restful Post, Java Restful URL


URL url = new URL("http://localhost:8080/healthbook/subject");
URLConnection connection = url.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Cache-Control", "no-cache");
OutputStreamWriter out = new OutputStreamWriter(
Gson gson = new Gson();
Subject subject = new Subject();
subject.setCreatedTime(new Date());
out.close(); br = new;
java.lang.StringBuffer sb = new java.lang.StringBuffer();
java.lang.String str = br.readLine();
while (str != null) {
str = br.readLine();
java.lang.String responseString = sb.toString();

Read More

HBase does not support row key renaming

As far as I know, in HBase, the key of the row can not be changed. But if you really need a row key rename function, the best thing to do is to copy all the data one row to another row in HBase using JAVA.

e.g. I have existing row with key “key1”, and I want to create a row with key “key2” copied from “key1” row. It is simple:

 // lets say your already got the result from table.get(Bytes.toBytes("key1")) 
Put put = new Put(Bytes.toBytes("key2")); 
NavigableMap&gt; familyQualifierMap = result.getNoVersionMap(); 
for (byte[] familyBytes : familyQualifierMap.keySet()) {
 NavigableMap qualifierMap = familyQualifierMap.get(familyBytes); 
for (byte[] qualifier : qualifierMap.keySet()) {
 put.add(familyBytes, qualifier, qualifierMap.get(qualifier)); } } 

Read More