tag:blogger.com,1999:blog-58092219285892069492024-03-28T03:59:31.251-07:00[Solved] ProblemThe main idea of the blog is to share solutions for problems I faced before.Anonymoushttp://www.blogger.com/profile/01232893302882499506noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-5809221928589206949.post-38260694241981040852017-06-16T12:11:00.000-07:002017-06-16T12:16:48.875-07:00Update or Modify PDF file on Ubuntu using Libreoffice If you want to edit or modify PDF or form on Ubuntu, you may find LibreOffice is a good tool to try before investigating other tools.<br />
<br />
Simply, open a terminal (Ctrl + Alt + T) and navigate to your PDF path. Then, type the following:<br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">libreoffice <pdf_name.pdf></span><br />
<br />
<br />[Solved] Problemhttp://www.blogger.com/profile/14621059585607406277noreply@blogger.com0tag:blogger.com,1999:blog-5809221928589206949.post-56207513152639792012014-12-07T09:27:00.003-08:002014-12-07T09:28:18.909-08:00Installing JDK 1.7 on ubuntu serverFollow the following steps to install JDK 1.7 on ubuntu server:<br />
<br />
<br />
1. Based on your linux architecture, download the proper version from Oracle website (<a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html" target="_blank">Oracle JDK 1.7</a>)<br />
<div>
<br />
<div>
2. Then, uncompress the jdk archive using the following command: </div>
<div>
<br /></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">tar -xvf jdk-7u65-linux-i586.tar</span><br />
Or using the following command for 64 bits:<br />
<span style="font-family: Courier New, Courier, monospace;"> tar -xvf jdk-7u65-linux-x64.tar</span><br />
<br />
3. Create a folder named jvm under (if not exists) using the following command </div>
<div>
<span style="font-family: Courier New, Courier, monospace;">sudo mkdir -p /usr/lib/jvm</span><br />
4. Then, move the extracted directory to /usr/lib/jvm:</div>
<div>
<span style="font-family: Courier New, Courier, monospace;">sudo mv ~/Downloads/jdk1.7.0_71 /usr/lib/jvm/</span><br />
5. Run the following commands to update the execution alternatives:</div>
<br />
<span style="font-family: Courier New, Courier, monospace;">sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0_71/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_71/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0_71/bin/javaws" 1</span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: inherit;">6. Finally, you need to export JAVA_HOME variable:</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<span style="font-family: Courier New, Courier, monospace;">export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_71</span><br />
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">or it is better to set JAVA_HOME in .bashrc:</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">nano ~/.bashrc </span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: inherit;">then add the same line:</span></div>
<div>
<span style="font-family: inherit;"><br /></span></div>
<div>
<span style="font-family: Courier New, Courier, monospace;">export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_71</span><br />
<div>
<br /></div>
</div>
[Solved] Problemhttp://www.blogger.com/profile/14621059585607406277noreply@blogger.com0tag:blogger.com,1999:blog-5809221928589206949.post-55699960946726333682014-06-14T18:09:00.000-07:002014-06-14T18:09:03.452-07:00How to download the source code of test jars using GradleI just faced a problem while trying downloading the source code of some of test jars. You just need to specify "test-sources" into your classifier.<br />
<br />
For example:<br />
<br />
<b>compile group:'org.xyz', name:'your-target-main-jar', version:'jar-version', classifier: 'test-sources'</b><br />
<br />
This will download your target main jar (of course with its dependencies) and its test jar associated with the source code.<br />
<br />
Also if you are using eclipse, you will need to add the following:<br />
<br />
<b>eclipse {</b><br />
<b> classpath {</b><br />
<b> downloadSources=true</b><br />
<b> }</b><br />
<b>}</b><br />
<br />
<br />[Solved] Problemhttp://www.blogger.com/profile/14621059585607406277noreply@blogger.com0tag:blogger.com,1999:blog-5809221928589206949.post-24431113642700601642013-11-26T19:08:00.000-08:002013-11-26T19:13:25.516-08:00Installing VMWare Player on Ubuntu 13.04<div dir="ltr" style="text-align: left;" trbidi="on">
I did not find a way to install VMWare player on Ubuntu 13.04 - 64 bits. But, I found an archived site having a VMWare player 6.0.0 for ubuntu 64-bits.<br />
To install VMWare 6.0.0, we need to install install VMWare 5.0.3 first and then the automatic update will do the rest for 6.0.0 version. To do that, open a terminal and run the following commands:<br />
<br />
<span style="background-color: white; line-height: 20px; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">sudo apt-get install build-essential linux-headers-`uname -r`</span></span><br />
<span style="background-color: white; line-height: 20px; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;"><br /></span></span>
<span style="background-color: white; line-height: 20px; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;">mkdir ~/VMware</span></span><br />
<span style="background-color: white; line-height: 20px; text-align: justify;"><span style="font-family: Courier New, Courier, monospace;"><br /></span></span>
<span style="background-color: white; line-height: 20px; text-align: justify; text-indent: 20px;"><span style="font-family: Courier New, Courier, monospace;">cd ~/VMware</span></span><br />
<span style="background-color: white; line-height: 20px; text-align: justify; text-indent: 20px;"><span style="font-family: Courier New, Courier, monospace;"><br /></span></span>
<span style="font-family: Courier New, Courier, monospace;"><span style="background-color: white; line-height: 20px; text-align: justify; text-indent: 20px;">wget -c </span><span style="line-height: 20px;">https://softwareupdate.vmware.com/cds/vmw-desktop/player/5.0.3/1410761/linux/core/VMware-Player-5.0.3-1410761.x86_64.bundle.tar</span></span><br />
<span style="font-family: Courier New, Courier, monospace;"><span style="line-height: 20px;"><br /></span></span>
<span style="font-family: Courier New, Courier, monospace;"><span style="background-color: white; line-height: 20px; text-align: justify; text-indent: 20px;">tar -xvf </span><span style="line-height: 20px;">VMware-Player-5.0.3-1410761.x86_64.bundle.tar</span></span><br />
<span style="font-family: Courier New, Courier, monospace;"><span style="line-height: 20px;"><br /></span></span>
<br />
<div style="text-align: left; text-indent: 0px;">
<span style="font-family: Courier New, Courier, monospace;"><span style="background-color: white; line-height: 20px; text-align: justify; text-indent: 20px;">chmod +x </span><span style="line-height: 20px;">VMware-Player-5.0.3-1410761.x86_64.bundle</span></span></div>
<div style="text-align: left; text-indent: 0px;">
<span style="font-family: Courier New, Courier, monospace;"><span style="line-height: 20px;"><br /></span></span></div>
<div style="text-align: left; text-indent: 0px;">
<span style="line-height: 20px;"><span style="font-family: Courier New, Courier, monospace;">sudo ./VMware-Player-5.0.3-1410761.x86_64.bundle</span></span></div>
<div style="text-align: left; text-indent: 0px;">
<span style="font-family: Tahoma, Arial, Helvetica;"><span style="font-size: 12px; line-height: 20px;"><br /></span></span></div>
<div style="text-align: left; text-indent: 0px;">
<span style="font-family: Tahoma, Arial, Helvetica;"><span style="font-size: 12px; line-height: 20px;">Then after the installation, you can type "VMWare" in the start menu then it will ask you to upgrade to VMWare 6.0.0 version.</span></span></div>
</div>
Anonymoushttp://www.blogger.com/profile/01232893302882499506noreply@blogger.com0tag:blogger.com,1999:blog-5809221928589206949.post-23029789640245694792012-05-19T05:41:00.000-07:002014-12-07T09:34:04.168-08:00Installing Hadoop on Ubuntu (Linux) - single node - Problems you may face<div dir="ltr" style="text-align: left;" trbidi="on">
This is not a new post, it is based on Michael G. Noll blog about <a href="http://www.michael-noll.com/tutorials/running-hadoop-on-ubuntu-linux-single-node-cluster/" target="_blank">Running Hadoop on Ubuntu (Single Node)</a><br />
I will go through the same steps, but I will point out some exceptions/errors you may face.<br />
<br />
Because I am a very new user of Ubuntu, this post is mainly targeting the Windows users and they have very primitive knowledge about Linux. I may write some hints in linux which seems very trivial for linux geeks, but it may be fruitful for Windows users.<br />
<br />
Moreover, I am assuming that you have enough knowledge about <u><b>HDFS architecture</b></u>. You can read <a href="http://hadoop.apache.org/docs/r0.18.0/hdfs_design.pdf" rel="nofollow" target="_blank">this document</a> for more details.<br />
<br />
I have used Ubuntu 11.04 and Hadoop 0.20.2.<br />
<br />
<div style="text-align: left;">
<h3 style="text-align: left;">
<b>Prerequisites:</b></h3>
<div>
<b><br /></b></div>
<div>
<b><br /></b></div>
<b>1. Installing Sun JDK 1.6: </b>Installing JDK is a required step to install Hadoop. You can follow the steps in my previous <a href="http://mysolvedproblem.blogspot.com/2012/05/installing-jdk-16-on-ubuntu.html" target="_blank">post</a>.<br />
<br />
<b>Update</b><br />
There is another simpler way to install JDK (for example installing JDK 1.7) using the instructions on this <a href="http://mysolvedproblem.blogspot.ca/2014/12/installing-jdk-17-on-ubuntu-server.html" target="_blank">post.</a> </div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b>2. Adding a dedicated Hadoop system user: </b>You will need a user for hadoop system you will install. To create a new user "hduser" in a group called "hadoop", run the following commands in your terminal:</div>
<div style="text-align: left;">
<br />
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$sudo addgroup hadoop</span></div>
<div style="text-align: left;">
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$sudo adduser --ingroup hadoop hduser</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: 'Courier New', Courier, monospace;">3.</span><b style="font-family: Times, 'Times New Roman', serif;">Configuring SSH: </b><span style="font-family: Times, 'Times New Roman', serif;">in Michael Blog, he assumed that the SSH is already installed. But if you didn't install SSH server before, you can run the following command in your terminal: </span><span style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;">By this command, you will have installed ssh server on your machine, the port is 2</span><span style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;">2 by default.</span><br />
<span style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;"><br /></span></div>
<div style="text-align: left;">
<pre style="margin: 0em; padding: 0px; text-align: -webkit-auto; width: 530px;"><span style="color: #333333;"><span style="background-color: #eeeeee; font-size: 14px;"><span style="font-family: Times, 'Times New Roman', serif;"> </span><span style="font-family: 'Courier New', Courier, monospace;">$sudo apt-get install openssh-server</span></span></span></pre>
<pre style="background-color: #fbfbfb; margin: 0em; padding: 0px; text-align: -webkit-auto; width: 530px;"><span style="color: #333333; font-family: Times, 'Times New Roman', serif;"><span style="font-size: 14px;">
</span></span></pre>
<pre style="background-color: #fbfbfb; margin: 0em; padding: 0px; text-align: -webkit-auto; width: 530px;"><span style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px;">We have installed SSH because Hadoop requires access to localhost (in case single node cluster) or communicates with remote nodes (in case multi-node cluster).</span></pre>
</div>
<div style="text-align: left;">
<div style="text-align: left;">
<span style="background-color: #fbfbfb; color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;">After this step, you will need to generate SSH key for hduser (and the users you need to administer Hadoop if any) by running the following commands, but you need first to switch to hduser:</span></div>
<span style="background-color: #fbfbfb; color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;"><br /></span>
<span style="background-color: #eeeeee; font-size: 14px; text-align: -webkit-auto;"><span style="color: #333333; font-family: 'Courier New', Courier, monospace;">$su - hduser</span></span><br />
<span style="background-color: #eeeeee; font-size: 14px; text-align: -webkit-auto;"><span style="color: #333333; font-family: 'Courier New', Courier, monospace;">$ssh-keygen -t rsa -P ""</span></span><br />
<span style="background-color: #fbfbfb; font-size: 14px; text-align: -webkit-auto;"><span style="color: #333333; font-family: 'Courier New', Courier, monospace;"><br /></span></span>
<span style="background-color: #fbfbfb; font-size: 14px; text-align: -webkit-auto;"><span style="color: #333333; font-family: Times, 'Times New Roman', serif;">To be sure that SSH installation is went well, you can open a new terminal and try to create ssh session using hduser by the following command:</span></span><br />
<span style="background-color: #fbfbfb; font-size: 14px; text-align: -webkit-auto;"><span style="color: #333333; font-family: 'Courier New', Courier, monospace;"><br /></span></span>
<span style="background-color: #eeeeee; font-size: 14px; text-align: -webkit-auto;"><span style="color: #333333; font-family: 'Courier New', Courier, monospace;">$ssh localhost</span></span><br />
<b style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;"></b><br />
<div style="text-align: left;">
<b style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;"><b style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;"><br /></b></b></div>
<b style="color: #333333; font-family: Times, 'Times New Roman', serif; font-size: 14px; text-align: -webkit-auto;">
4. Disable IPv6: </b>You will need to disable IP version 6 because Ubuntu is using 0.0.0.0 IP for different Hadoop configurations. You will need to run the following commands using a root account:<br />
$sudo gedit /etc/sysctl.conf</div>
<div style="text-align: left;">
This command will open sysctl.conf in text editor, you can copy the following lines at the end of the file:</div>
<div style="text-align: left;">
<div>
<br /></div>
<div>
<span style="background-color: #eeeeee;">#disable ipv6</span></div>
<div>
<span style="background-color: #eeeeee;">net.ipv6.conf.all.disable_ipv6 = 1</span></div>
<div>
<span style="background-color: #eeeeee;">net.ipv6.conf.default.disable_ipv6 = 1</span></div>
<div>
<span style="background-color: #eeeeee;">net.ipv6.conf.lo.disable_ipv6 = 1</span></div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYCVPBzrt6hFY5WylwL0Fh_7dVug2_k_Y8GFPfB6Zb0gDDlpdLyl3TP8BQfpXasD727j359OYuC5z_sMYZsfldNElQlsBasWx14s6WA-qjhKXlFLWCKxlX4K5jbzi8xTGPJpmRl5n4fz9A/s1600/Disable+IP+V6.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Disable IP V6" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYCVPBzrt6hFY5WylwL0Fh_7dVug2_k_Y8GFPfB6Zb0gDDlpdLyl3TP8BQfpXasD727j359OYuC5z_sMYZsfldNElQlsBasWx14s6WA-qjhKXlFLWCKxlX4K5jbzi8xTGPJpmRl5n4fz9A/s400/Disable+IP+V6.jpg" height="335" title="[Solved] Problem Hadoop Installation Ubuntu Single Node Disable IP V6" width="400" /></a></div>
<div>
<br /></div>
<br />
<div>
You can save the file and close it. If you faced a problem telling you don't have permissions, just remember to run the previous commands by your root account.<br />
<br />
These steps required you to reboot your system, but alternatively, you can run the following command to re-initialize the configurations again.<br />
<br />
<span style="background-color: #eeeeee;"><span style="font-family: 'Courier New';">$sudo sysctl -p</span> </span><br />
<br />
To make sure that IPV6 is disabled, you can run the following command:<br />
<br />
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$cat /proc/sys/net/ipv6/conf/all/disable_ipv6</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="font-family: Times, 'Times New Roman', serif;">The printed value should be 1, which means that is disabled.</span><br />
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span>
<br />
<h3 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif; font-size: large;"><b>Installing Hadoop</b></span></h3>
<div>
<span style="font-family: Times, 'Times New Roman', serif; font-size: large;"><b><br /></b></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Now we can download Hadoop to begin installation. Go to <a href="http://www.apache.org/dyn/closer.cgi/hadoop/core" target="_blank">Apache Downloads</a> and download Hadoop version 0.20.2. To overcome the security issues, you can download the tar file in hduser directory, for example, /home/hduser. Check the following snapshot:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL_USKV1mtsexX_R6rITh8J25A9ykH3cxpMsW7VbNSkma8-wrrT5oaSM55feu0hx5Wu7vUYs-1SoRmJL3I0jHfjli7rXAaDHRUoaS_c7INz2HxGxVUpfydE9fEQtSRMW4h56yuOkrMjUQS/s1600/Download+Hadoop.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Download Hadoop" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL_USKV1mtsexX_R6rITh8J25A9ykH3cxpMsW7VbNSkma8-wrrT5oaSM55feu0hx5Wu7vUYs-1SoRmJL3I0jHfjli7rXAaDHRUoaS_c7INz2HxGxVUpfydE9fEQtSRMW4h56yuOkrMjUQS/s400/Download+Hadoop.jpg" height="288" title="[Solved] Problem Hadoop Installation Ubuntu Single Node Download Hadoop" width="400" /></a></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Then you need to extract the tar file and rename the extracted folder to 'hadoop'. Open a new terminal and run the following command:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$ cd /home/hduser</span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$ sudo tar xzf hadoop-0.20.2.tar.gz</span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$ sudo mv hadoop-0.20.2 hadoop</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Please note if you want to grant access for another hadoop admin user (e.g. hduser2), you have to grant read permission to folder /home/hduser using the following command:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: #eeeeee;"><span style="color: #333333; font-family: 'andale mono', 'lucida console', monospace; font-size: 0.9em; line-height: 1.3;">sudo chown -R hduser2:hadoop hadoop</span><span style="font-family: Times, 'Times New Roman', serif;"> </span></span></div>
<h4 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif;"><b><br /></b></span></h4>
<h4 style="text-align: left;">
<b style="font-family: Times, 'Times New Roman', serif;">Update $HOME/.bashrc</b></h4>
<div>
<b style="font-family: Times, 'Times New Roman', serif;"><br /></b></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">You will need to update the .bachrc for hduser (and for every user you need to administer Hadoop). To open .bachrc file, you will need to open it as root:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: #eeeeee; font-family: Times, 'Times New Roman', serif;">$sudo gedit /home/hduser/.bashrc</span></div>
<div>
<span style="background-color: #eeeeee; font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: white; font-family: Times, 'Times New Roman', serif;">Then you will add the following configurations at the end of .bachrc file</span></div>
<div>
<span style="background-color: #eeeeee; font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: #eeeeee;"><span style="font-family: 'Courier New', Courier, monospace;"></span></span><br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;"># Set Hadoop-# related environment variables</span></span></div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;">export HADOOP_HOME=/home/hduser/hadoop</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;">
</span></span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;"><br /></span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;"># Set JAVA_HOME (we will also configure JAVA_HOME directly for Hadoop later on)</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;">export JAVA_HOME=/usr/lib/jvm/java-6-sun</span></span><br />
# or you can write the following command if you used this <a href="http://mysolvedproblem.blogspot.ca/2014/12/installing-jdk-17-on-ubuntu-server.html" target="_blank">post</a> to install your java<br />
# <span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_71</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;"><br /></span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;"><br /></span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;"># Some convenient aliases and functions for running Hadoop-related commands</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;">unalias fs &> /dev/null</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;">alias fs="hadoop fs"</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;">unalias hls &> /dev/null</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;">alias hls="fs -ls"</span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
</span>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><span style="background-color: #eeeeee;"><br /></span></span></div>
<span style="font-family: 'Courier New', Courier, monospace;">
<div>
<span style="background-color: #eeeeee;"># If you have LZO compression enabled in your Hadoop cluster and</span></div>
<div>
<span style="background-color: #eeeeee;"># compress job outputs with LZOP (not covered in this tutorial):</span></div>
<div>
<span style="background-color: #eeeeee;"># Conveniently inspect an LZOP compressed file from the command</span></div>
<div>
<span style="background-color: #eeeeee;"># line; run via:</span></div>
<div>
<span style="background-color: #eeeeee;">#</span></div>
<div>
<span style="background-color: #eeeeee;"># $ lzohead /hdfs/path/to/lzop/compressed/file.lzo</span></div>
<div>
<span style="background-color: #eeeeee;">#</span></div>
<div>
<span style="background-color: #eeeeee;"># Requires installed 'lzop' command.</span></div>
<div>
<span style="background-color: #eeeeee;">#</span></div>
<div>
<span style="background-color: #eeeeee;">lzohead () {</span></div>
<div>
<span style="background-color: #eeeeee;"> hadoop fs -cat $1 | lzop -dc | head -1000 | less</span></div>
<div>
<span style="background-color: #eeeeee;">}</span></div>
<div>
<span style="background-color: #eeeeee;"><br /></span></div>
<div>
<span style="background-color: #eeeeee;"># Add Hadoop bin/ directory to PATH</span></div>
<div>
<span style="background-color: #eeeeee;">export PATH=$PATH:$HADOOP_HOME/bin</span></div>
</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<h3 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif; font-size: large;"><b>Hadoop Configuration</b></span></h3>
<h3 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif; font-size: large;"><b> </b></span></h3>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Now, we need to configure Hadoop framework on Ubuntu machine. The following are configuration files we can use to do the proper configuration. To know more about hadoop configurations, you can visit <a href="http://wiki.apache.org/hadoop/GettingStartedWithHadoop" target="_blank">this site</a></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<h3 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif;">hadoop-env.sh</span></h3>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">We need only to update the JAVA_HOME variable in this file. Simply you will open this file using a text editor using the following command:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$sudo gedit /home/hduser/hadoop/conf/hadoop-env.sh</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Then you will need to change the following line</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"># export JAVA_HOME=/usr/lib/j2sdk1.5-sun</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">To </span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;">export JAVA_HOME=/usr/lib/jvm/java-6-sun</span><br />
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span>
or you can write the following command if you used this <a href="http://mysolvedproblem.blogspot.ca/2014/12/installing-jdk-17-on-ubuntu-server.html" target="_blank">post</a> to install your java<br />
# <span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_71</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Note: if you faced "<span style="font-size: 10.5pt; line-height: 115%;">Error: JAVA_HOME is not set</span>" Error while starting the services, then you seems that you forgot toe uncomment the previous line (just remove #).</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<h3 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif;">core-site.xml</span></h3>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">First, we need to create a temp directory for Hadoop framework. If you need this environment for testing or a quick prototype (e.g. develop simple hadoop programs for your personal test ...), I suggest to create this folder under /home/hduser/ directory, otherwise, you should create this folder in a shared place under shared folder (like /usr/local ...) but you may face some security issues. But to overcome the exceptions that may caused by security (like java.io.IOException), I have created the tmp folder under hduser space.</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">To create this folder, type the following command:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$ sudo mkdir /home/hduser/tmp</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Please note that if you want to make another admin user (e.g. hduser2 in hadoop group), you should grant him a read and write permission on this folder using the following commands:</span></div>
<div>
<span style="font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"></span><br />
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$ sudo chown hduser2:hadoop /home/hduser/tmp</span></div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">
</span>
<br />
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$ sudo chmod 755 /home/hduser/tmp</span></div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">
</span></div>
<div>
</div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Now, we can open hadoop/conf/core-site.xml to edit the hadoop.tmp.dir entry.</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">We can open the core-site.xml using text editor:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">$sudo gedit /home/hduser/hadoop/conf/core-site.xml</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">Then add the following configurations between <configuration> .. </configuration> xml elements:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"><!-- In: conf/core-site.xml --></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"><property></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> <name>hadoop.tmp.dir</name></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> <value>/home/hduser/tmp</value></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> <description>A base for other temporary directories.</description></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"></property></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"><property></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> <name>fs.default.name</name></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> <value>hdfs://localhost:54310</value></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> <description>The name of the default file system. A URI whose</span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> scheme and authority determine the FileSystem implementation. The</span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> uri's scheme determines the config property (fs.SCHEME.impl) naming</span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> the FileSystem implementation class. The uri's authority is used to</span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"> determine the host, port, etc. for a filesystem.</description></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"></property></span></div>
</div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"><br /></span>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<h3 style="text-align: left;">
<span style="background-color: white; font-family: Times, 'Times New Roman', serif;"><b>mapred-site.xml</b></span></h3>
<div>
<span style="background-color: white; font-family: Times, 'Times New Roman', serif;">We will open the hadoop/conf/mapred-site.xml using a text editor and add the following configuration values (like core-site.xml)</span></div>
<div>
<span style="background-color: white; font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"><!-- In: conf/mapred-site.xml --></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"><property></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> <name>mapred.job.tracker</name></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> <value>localhost:54311</value></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> <description>The host and port that the MapReduce job tracker runs</span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> at. If "local", then jobs are run in-process as a single map</span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> and reduce task.</span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> </description></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"></property></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"><br /></span>
<span style="background-color: #eeeeee;"><br /></span></div>
<h4 style="text-align: left;">
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;">hdfs-site.xml</span></span></h4>
</div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;">Open hadoop/conf/hdfs-site.xml using a text editor and add the following configurations:</span></span></div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"><!-- In: conf/hdfs-site.xml --></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"><property></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> <name>dfs.replication</name></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> <value>1</value></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> <description>Default block replication.</span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> The actual number of replications can be specified when the file is created.</span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> The default is used if replication is not specified in create time.</span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"> </description></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"></property></span></div>
<div style="font-family: 'Courier New', Courier, monospace;">
<span style="background-color: #eeeeee;"><br /></span>
<span style="background-color: #eeeeee;"><br /></span></div>
<h3 style="text-align: left;">
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;">Formatting NameNode</span></span></h3>
</div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;">You should format the NameNode in your HDFS. You should not do this step when the system is running. It is usually done once at first time of your installation.</span></span></div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;">Run the following command</span></span></div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">$/home/hduser/hadoop/bin/hadoop namenode -format</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwUGNq5_80YpRIZ6FUSlBtIO5bIeUyv0CfrlOK97ExGbgBIQ_eQiH5z5rld6s9UYbRMEKHv_YAldEkj2QolNFXw_KJjpd-dLCDt07q-D4TISbNbIIkONYAZ1F5HO__1iEl6fn9jWuR23af/s1600/Format+NameNode.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Formatting Namenode (HDFS)" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwUGNq5_80YpRIZ6FUSlBtIO5bIeUyv0CfrlOK97ExGbgBIQ_eQiH5z5rld6s9UYbRMEKHv_YAldEkj2QolNFXw_KJjpd-dLCDt07q-D4TISbNbIIkONYAZ1F5HO__1iEl6fn9jWuR23af/s400/Format+NameNode.jpg" height="256" title="Formatting Namenode (HDFS)" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">NameNode Formatting</td></tr>
</tbody></table>
<h3 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></h3>
<h3 style="text-align: left;">
<span style="font-family: Times, 'Times New Roman', serif;">Starting Hadoop Cluster</span></h3>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">You will need to navigate to hadoop/bin directory and run ./start-all.sh script.</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtvkhO3RdivrAPxrRfy0C0U-D96L9ewfhEebesttuAaHLHJPJ3mg2ZStPvfS2vJks4cHnqIWSpbUuVTsE0U1ampwjCd7GL0wMyFfhgzeWGtD2UxzS4OYotjn2_BWKVJbOS6IIi567_E2Fu/s1600/Starting+Hadoop+Services.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Starting Hadoop Services ./start-all.sh" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtvkhO3RdivrAPxrRfy0C0U-D96L9ewfhEebesttuAaHLHJPJ3mg2ZStPvfS2vJks4cHnqIWSpbUuVTsE0U1ampwjCd7GL0wMyFfhgzeWGtD2UxzS4OYotjn2_BWKVJbOS6IIi567_E2Fu/s400/Starting+Hadoop+Services.jpg" height="256" title="Starting Hadoop Services ./start-all.sh" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Starting Hadoop Services using ./start-all.sh</td></tr>
</tbody></table>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;">There is a nice tool called jps. You can use it to ensure that all the services are up.</span></span></div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSixOVkKsCTCZLJ3ZMozyLS_GFaedOym69qe5rRyoNmFLI9QmyavVlY-JKgN2mNtAjiWZGLb_z1K_hu7_mtRF89_rWia7pmrW8y257f2Bgqr8FSc5kV3Rjq5-e5ygh5iV6I9erHdI-Qmk9/s1600/JPS.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="jps tool" border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSixOVkKsCTCZLJ3ZMozyLS_GFaedOym69qe5rRyoNmFLI9QmyavVlY-JKgN2mNtAjiWZGLb_z1K_hu7_mtRF89_rWia7pmrW8y257f2Bgqr8FSc5kV3Rjq5-e5ygh5iV6I9erHdI-Qmk9/s400/JPS.jpg" height="256" title="jps tool" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Using jps tool</td></tr>
</tbody></table>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;"><b><br /></b></span></span>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;"><b>Running an Example (Pi Example)</b></span></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;">There are many built-in examples. We can run PI estimator example using the following command:</span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><br /></span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;">hduser@ubuntu:~/hadoop/bin$ hadoop jar ../hadoop-0.20.2-examples.jar pi 3 10</span></div>
<div>
<span style="background-color: #eeeeee; font-family: 'Courier New', Courier, monospace;"><br /></span></div>
<div>
<span style="font-family: Times, 'Times New Roman', serif;"><span style="background-color: white;">If you faced "</span><span style="font-size: 11pt; line-height: 115%;">Incompatible namespaceIDs</span><span style="background-color: white;">" Exception you can do the following:</span></span></div>
<div>
<span style="background-color: white;"><span style="font-family: Times, 'Times New Roman', serif;"><br /></span></span></div>
<div>
<div class="MsoPlainText" style="direction: ltr; margin-left: 0.5in; text-indent: -0.25in; unicode-bidi: embed;">
<span style="font-family: Times, 'Times New Roman', serif;">1.<span style="font-size: 7pt;"> </span><span dir="LTR"></span>Stop all the services (by calling ./stop-all.sh).<o:p></o:p></span></div>
<div class="MsoPlainText" style="direction: ltr; margin-left: 0.5in; text-indent: -0.25in; unicode-bidi: embed;">
<span style="font-family: Times, 'Times New Roman', serif;">2.<span style="font-size: 7pt;"> </span><span dir="LTR"></span>Delete
/tmp/hadoop/dfs/data/*<o:p></o:p></span></div>
<div class="MsoPlainText" style="direction: ltr; margin-left: 0.5in; text-indent: -0.25in; unicode-bidi: embed;">
<span style="font-family: Times, 'Times New Roman', serif;">3.<span style="font-size: 7pt;"> </span><span dir="LTR"></span>Start all the services.</span><span style="font-family: 'Courier New';"><o:p></o:p></span></div>
</div>
</div>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/01232893302882499506noreply@blogger.com162tag:blogger.com,1999:blog-5809221928589206949.post-52605069347890745922012-05-13T00:37:00.000-07:002012-05-13T00:37:16.968-07:00Java Performance Tips 2<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><u><span style="font-size: 13pt; line-height: 150%;">Introduction<o:p></o:p></span></u></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">We
talked about Strings operations in the last article, now we will talk about
more tips on performance tips, we will talk about the importance of creating
stateless methods and class if your logic doesn't depend on the state of the
object, also we will talk about object reusing and how will affect the
performance by reducing the headache of garbage collection.<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><u><span style="font-size: 13pt; line-height: 150%;"><br /></span></u></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><u><span style="font-size: 13pt; line-height: 150%;">Take
'State or Stateless' Decision<o:p></o:p></span></u></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">Creating
and destroying objects issue in java can cause performance issues, for example
creating State classes for stateless data; a stateless class means that the
data of the created object doesn't depend on the state where there is no fields
or attributes that makes an object differs from other.</span><span style="font-size: 13pt; line-height: 150%;"> <o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">We can imagine that the Stateless class can say 'Use
me only if your code doesn't depend on your object state, All the objects are
functionally equivalent to me, you will gain besides that no creation of
objects, the CPU will be happy of doing that </span><span style="font-family: Wingdings; font-size: 13pt; line-height: 150%;">J</span><span style="font-size: 13pt; line-height: 150%;">' <o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br />
<br />
<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;">Code Example<o:p></o:p></span></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;"><br /></span></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">The following </span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">StatelessClassWithoutStaticMethods</span><span style="font-size: 13pt; line-height: 150%;"> class has a
method called execute, where the logic of this method doesn't depend on the
state of the object, while it is not static method.<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">package</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
performancetest.episode2;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">class</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> StatelessClassWithoutStaticMethods {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> execute()</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><span style="color: #3f7f5f; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">// the logic here doesn't depend on
the object state</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">for</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> (</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">int</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
i = 0; i < 10000; i++) {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> System.</span><i><span style="color: #0000c0; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">out</span></i><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">.println(i);</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">}</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">While the right version as the
following:<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">package</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
performancetest.episode2;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">class</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> StatelessClassWithStaticMethods {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">static</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
execute()</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><span style="color: #3f7f5f; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">// the logic here doesn't depend on
the object state</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">for</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> (</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">int</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
i = 0; i < 10000; i++) {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> System.</span><i><span style="color: #0000c0; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">out</span></i><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">.println(i);</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">}</span><span style="font-size: 13pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;"><br /></span></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;"><br /></span></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;">Performance Analysis<o:p></o:p></span></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">Using TBTB (Eclipse Test & Performance Tools
Platform Project), the following results are shown as follows <o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><o:p></o:p></span></div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGfOwJHMVh2TIPDhkNRPLMkXzkv7L8B3VZOhMcCY3NDxQTkMcyW6qqf9CazaA85ssJuV_N8fX0bXXAfMWYV4AjkWSWWJAoeb_WyuAA4k7CR1k44flj5YdIgyA9I3gxFgcph-MxLBJkC1T2/s1600/Performance+Analysis.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img alt="Java Performance Analysis" border="0" height="150" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGfOwJHMVh2TIPDhkNRPLMkXzkv7L8B3VZOhMcCY3NDxQTkMcyW6qqf9CazaA85ssJuV_N8fX0bXXAfMWYV4AjkWSWWJAoeb_WyuAA4k7CR1k44flj5YdIgyA9I3gxFgcph-MxLBJkC1T2/s400/Performance+Analysis.JPG" title="Java Performance Analysis" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Performance Analysis</td></tr>
</tbody></table>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">Based
on this figure, the static execute method has loss approximately 18% of time
from the non-static one.<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">That
doesn't mean that you have to make all the class a static (i.e. a class has
static methods only), but if you have a state method in this class depends on
the object state, make this method only non-static.<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<br /></div>
<span style="font-family: Calibri, sans-serif; font-size: 13pt; line-height: 150%;"><br clear="all" style="page-break-before: always;" />
</span>
<br />
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><u><span style="font-size: 13pt; line-height: 150%;">Don't
Create A New One If You Can What You Have<o:p></o:p></span></u></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">'You
can use me before I go to the garbage collection, clear my data and reuse me
again; we want to lessen the work on our friend garbage collection' Any object
says this quote </span><span style="font-family: Wingdings; font-size: 13pt; line-height: 150%;">J</span><span style="font-size: 13pt; line-height: 150%;">, Creating new objects is expensive as we know as more
objects we create, the garbage collection job will be more time consuming.<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">So if
you have a chance to reuse the object again without creating a new one of the
same type, do it immediately, it will improve the performance and will make the
garbage collection job be easier.<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;">Code
Example<o:p></o:p></span></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; text-indent: 0.5in; unicode-bidi: embed;">
<b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">static</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
fillVectorDataWithoutRecycling()</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">for</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> (</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">int</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
i = 0 ; i < 1000 ; i ++)</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>Vector</u> v = </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">new</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
<u>Vector</u>();</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>v.add(</u></span><u><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">"item1"</span></u><u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">)</span></u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>v.add(</u></span><u><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">"item2"</span></u><u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">)</span></u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>v.add(</u></span><u><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">"item3"</span></u><u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">)</span></u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> System.</span><i><span style="color: #0000c0; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">out</span></i><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">.println(v.get(0));</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">public</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">static</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">void</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
fillVectorDataWithRecycling()</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>Vector</u> v = </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">new</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
<u>Vector</u>();</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> </span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">for</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> (</span><b><span style="color: #7f0055; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">int</span></b><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">
i = 0 ; i < 1000 ; i ++)</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> {</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> v.clear();</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>v.add(</u></span><u><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">"item1"</span></u><u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">)</span></u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>v.add(</u></span><u><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">"item2"</span></u><u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">)</span></u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> <u>v.add(</u></span><u><span style="color: #2a00ff; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">"item3"</span></u><u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">)</span></u><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">;</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> System.</span><i><span style="color: #0000c0; font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">out</span></i><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;">.println(v.get(0));</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }</span><span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt; line-height: 150%;"> }<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">We have
two methods, fillVectorDataWithoutRecycling method creates a new vector object
in the loop, while fillVectorDataWithRecycling method uses one vector object
and clears the vector object and reuse it.<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;"><br /></span></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<b><span style="font-size: 13pt; line-height: 150%;">Performance
Analysis<o:p></o:p></span></b></div>
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtY2SqIg1RM-YGHylpjMebt1QSdRy0ji9rMkn3b-WpwRmcLwpal3ortgyMhFRPOwtThRJvsspuOVaaEQ6DjF8IJWiICVsCTwDNN1lHsUYW5UnlG_7MI7KfQCaSxDe2FDGI1n7fM4DZb2lo/s1600/Performance+Analysis+2.JPG" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img alt="Java Performance Analysis With/Without Vector Recycling" border="0" height="200" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtY2SqIg1RM-YGHylpjMebt1QSdRy0ji9rMkn3b-WpwRmcLwpal3ortgyMhFRPOwtThRJvsspuOVaaEQ6DjF8IJWiICVsCTwDNN1lHsUYW5UnlG_7MI7KfQCaSxDe2FDGI1n7fM4DZb2lo/s640/Performance+Analysis+2.JPG" title="Java Performance Analysis With/Without Vector Recycling" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Performance Analysis With/Without Vector Recycling</td></tr>
</tbody></table>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-size: 17px; line-height: 25px;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;"><br /></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; unicode-bidi: embed;">
<span style="font-size: 13pt; line-height: 150%;">From
this figure we found that reusing the current object will cause loss approximately
50% of time (of course without taking in the consideration the hardware
architecture like CPU, cache and memory).<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; line-height: 150%; margin-bottom: 0.0001pt; text-indent: 0.5in; unicode-bidi: embed;">
<br /></div>
</div>Anonymoushttp://www.blogger.com/profile/01232893302882499506noreply@blogger.com0tag:blogger.com,1999:blog-5809221928589206949.post-52487322534263772842012-05-13T00:35:00.001-07:002012-07-25T01:21:12.085-07:00Java Performance Tips 1 - String Operations<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<b><u><span style="font-size: 13pt;">String
Operations<o:p></o:p></span></u></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
There are some performance tips for String
manipulation in java, one of them is the concatenation operation.</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
Concatenation operation can be done by
appending the values on the same String object for example:</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
String str = new String ();<br />
String s = "test";<br />
str+=s;</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
str+="testString";</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
The compiler translated this simple line to
the following <span style="font-family: Wingdings;">J</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; margin-bottom: 0.0001pt; unicode-bidi: embed;">
<span style="font-family: 'Courier New'; font-size: 10pt;">str = (new
StringBuffer()).append(s).append("</span>testString<span style="font-family: 'Courier New'; font-size: 10pt;">").toString();<o:p></o:p></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
But this method is not preferable (as we will
know later), one other method is using StringBuffer .</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<b><br /></b><br />
<b>Using StringBuffer Method <o:p></o:p></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
StringBuffer is used to store character
strings that will be changed as we know that String class is immutable, so we
can concatenate the strings as follows:</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
StringBuffer sbuffer = new StringBuffer();</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
sbuffer.append("testString");</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<b>StringBuffer vs StringBuilder<o:p></o:p></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
Also there is another method to concatenate
the String using StringBuilder which is introduced in Java 5, StringBuilder is
like the StringBuffer except it is not synchronized, which means that if there
are many threads, they can change it in the same time (StringBuilder is not
suitable in the multithreading applications).</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
'Ok, why this stuff for, just for concatenate
some strings!' you may ask this question, after running a sample of each and
profiling the performance.</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<b>Code Example<o:p></o:p></b><br />
<b><br /></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">public class StringOperations {</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
public void concatenateUsingString() {</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
String str = new String();</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
for (int i = 0; i < 10000; i++) {</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
str += "testString";</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
}<span dir="RTL" lang="AR-SA"><o:p></o:p></span></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;"><span dir="LTR"></span><span dir="LTR"></span> }<span dir="RTL" lang="AR-SA"><o:p></o:p></span></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;"><span dir="LTR"></span><span dir="LTR"></span> public void concatenateUsingStringBuffer()
{</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
StringBuffer sbuffer = new StringBuffer();</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
for (int i = 0; i < 10000; i++) {</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
sbuffer.append("testString");</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
}<span dir="RTL" lang="AR-SA"><o:p></o:p></span></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;"><span dir="LTR"></span><span dir="LTR"></span> }<span dir="RTL" lang="AR-SA"><o:p></o:p></span></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;"><span dir="LTR"></span><span dir="LTR"></span> public void concatenateUsingStringBuilder()
{</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
StringBuilder sbuilder = new StringBuilder();</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
for (int i = 0; i < 10000; i++) {</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
sbuilder.append("testString");</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
}<span dir="RTL" lang="AR-SA"><o:p></o:p></span></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;"><span dir="LTR"></span><span dir="LTR"></span> }<span dir="RTL" lang="AR-SA"><o:p></o:p></span></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span dir="LTR"></span><span dir="LTR"></span>}</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
And in the main method, a simple calling to
the three methods <br />
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">public static void main(String[] args) {</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
StringOperations soperations = new StringOperations(); <span dir="RTL" lang="AR-SA"><o:p></o:p></span></span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;"><span dir="LTR"></span><span dir="LTR"></span> soperations.concatenateUsingString();</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
soperations.concatenateUsingStringBuffer();</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;">
soperations.concatenateUsingStringBuilder();</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New', Courier, monospace;"> }</span></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
The result is like the following:<br />
<b style="text-indent: 0.5in;">The Results by seconds</b><br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZ38kazKFl6nTAmgX5QlBmJ179fT-y0C-HkPQDImPswItEB6aN0U8Jj_ulF-Ifwy6NT_6PPGykdCx9sDReS3faMQlt-cTUri47pfIFAMlG7QUrSJCJ1ykezrNJ_pjeslDZs4kiOSb6qkVv/s1600/The+results+per+second.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="97" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhZ38kazKFl6nTAmgX5QlBmJ179fT-y0C-HkPQDImPswItEB6aN0U8Jj_ulF-Ifwy6NT_6PPGykdCx9sDReS3faMQlt-cTUri47pfIFAMlG7QUrSJCJ1ykezrNJ_pjeslDZs4kiOSb6qkVv/s400/The+results+per+second.jpg" width="400" /></a></div>
<b style="text-indent: 0.5in;"><br /></b></div>
<div align="center" class="MsoNormal" dir="LTR" style="direction: ltr; text-align: center; unicode-bidi: embed;">
<b><br /></b><br />
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
<b>The
Results by percentage</b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div class="separator" style="clear: both; text-align: left;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixmyJOe86aUgCmFnOmEVr2gSPzKHmim3Nh3qcQO2ozS6jFrfYzbww19GU_jyToHB3YbiVJVAUDZolod9ICchb3YHAcwADDc-bMV4vzFd4slUpvyYAxUVgM_1IY-Ir1oEolpsNWkZUG0WpK/s1600/Relative+Percentages.JPG" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="96" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixmyJOe86aUgCmFnOmEVr2gSPzKHmim3Nh3qcQO2ozS6jFrfYzbww19GU_jyToHB3YbiVJVAUDZolod9ICchb3YHAcwADDc-bMV4vzFd4slUpvyYAxUVgM_1IY-Ir1oEolpsNWkZUG0WpK/s400/Relative+Percentages.JPG" width="400" /></a></div>
<div style="text-align: left;">
<b><br /></b></div>
</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
I have used
the Eclipse Test & Performance Tools Platform Project <span class="apple-style-span"><span style="font-family: Arial, sans-serif; font-size: 9pt;">(TPTP)</span></span> to validate
the results, just right click on the project and choose 'Profile As'.<br />
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTm3qbEXPKtfUJ95Zr7y3klXKLA-dJCDZ00KCv8tL0QrQqVq6tvMY1-gDNyhyphenhyphenP4pjSEXdUkuJMfdNd1LP_N-uig8A20j_8rtwjWjrkzOvyU-LmwXn3q_Mq0NmeIHpladrJU1EmbGESXtTu/s1600/Profile+As.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img alt="Profile As" border="0" height="203" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTm3qbEXPKtfUJ95Zr7y3klXKLA-dJCDZ00KCv8tL0QrQqVq6tvMY1-gDNyhyphenhyphenP4pjSEXdUkuJMfdNd1LP_N-uig8A20j_8rtwjWjrkzOvyU-LmwXn3q_Mq0NmeIHpladrJU1EmbGESXtTu/s320/Profile+As.jpg" title="Choosing Profile As" width="320" /></a></div>
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Then
choose 'Profile<span style="font-family: Wingdings;">à</span>Profile
Configuration '<br />
<br /></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<br />
<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: left; margin-right: 1em; text-align: left;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRB5IKdAcCy5MblMhCsqGHbGnHDEx0uS4ABdutyolMrxm4eK7YLiZB_M3lks-UQ6I1QDxp471QeVmtKwozGN-oW-xkX7GoxO_wfX94oZJMpn6kWTx35rnerj9q8rtM5mlBR6QsCquQCQ0U/s1600/Execution+Time.jpg" imageanchor="1" style="clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;"><img border="0" height="256" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjRB5IKdAcCy5MblMhCsqGHbGnHDEx0uS4ABdutyolMrxm4eK7YLiZB_M3lks-UQ6I1QDxp471QeVmtKwozGN-oW-xkX7GoxO_wfX94oZJMpn6kWTx35rnerj9q8rtM5mlBR6QsCquQCQ0U/s320/Execution+Time.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Profile Configuration</td></tr>
</tbody></table>
<br />
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
<br /></div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
This result shows the big performance issue of using the
String concatenation (Plus operation),</div>
<div class="MsoNormal" style="direction: ltr; unicode-bidi: embed;">
the profiler tells us that the calling of concatenateUsingStringBuffer and
concatenateUsingStringBuilder (approximately 0.08%) of time are nothing with
respect to concatenateUsingString (99.85% of time).</div>
</div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
<b><br /></b><br />
<h2 style="text-align: left;">
<b><a href="http://mysolvedproblem.blogspot.com/2012/05/java-performance-tips-2.html" target="_blank"><span style="color: purple;">Now you can go to Java Performance Tips 2 article</span></a></b></h2>
<b><br /></b><br />
<b>References<o:p></o:p></b></div>
<div class="MsoNormal" dir="LTR" style="direction: ltr; unicode-bidi: embed;">
http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html <br />
http://leepoint.net/notes-java/data/strings/23stringbufferetc.html<br />
<br />
<br />
<br />
<br /></div>
</div>Anonymoushttp://www.blogger.com/profile/01232893302882499506noreply@blogger.com8tag:blogger.com,1999:blog-5809221928589206949.post-17944596344143198192012-05-11T01:33:00.000-07:002012-05-18T13:05:25.169-07:00Installing JDK 1.6 on Ubuntu<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div>
<ul>
<li><b>Installing Sun JDK 1.6: </b>You can download oab-java6 zip file from this <a href="https://github.com/flexiondotorg/oab-java6" target="_blank">URL</a> , then you will need to extract this zip file and open the terminal (pressing ctrl+alt+t) to run ./oab-java6.sh. You must be run this command as sudo (using a root account).</li>
</ul>
<br /><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV51ncgNwhUPJoOUSbb-tr3DmFHyhtVY8qqVu2wvsyu8PTECNfDbFaaOGBHojSiV3Vwb49D7dTVjfd6JDfKZEknb4DPopIkRUuafuvaBSvnknuDmopHff8CCpaiVkSKBvkTxvMq8AD3NN2/s1600/install+jdk6.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Installing JDK 1.6 on Ubuntu" border="0" height="256" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjV51ncgNwhUPJoOUSbb-tr3DmFHyhtVY8qqVu2wvsyu8PTECNfDbFaaOGBHojSiV3Vwb49D7dTVjfd6JDfKZEknb4DPopIkRUuafuvaBSvnknuDmopHff8CCpaiVkSKBvkTxvMq8AD3NN2/s400/install+jdk6.JPG" title="Installing JDK 1.6 on Ubuntu" width="400" /></a></div>
<div>
<b><br /></b></div>
</div>
<div style="text-align: -webkit-auto;">
This script is doing the following:</div>
<div style="border: 0px; color: #333333; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 14px; line-height: 22px; margin-bottom: 15px; margin-top: 15px; padding: 0px; text-align: -webkit-auto;">
The basic execution steps are:</div>
<ul>
<li>Remove, my now disabled, Java PPA 'ppa:flexiondotorg/java'.</li>
<li>Install the tools required to build the Java packages.</li>
<li>Create download cache in /var/local/oab/pkg.</li>
<li>Download the i586 and x64 Java install binaries from Oracle. Yes, both are required.</li>
<li>Clone the build scripts from <a href="https://github.com/rraptorr/sun-java6">https://github.com/rraptorr/sun-java6</a></li>
<li>Build the Java packages applicable to your system.</li>
<li>Create local apt repository in /var/local/oab/deb for the newly built Java Packages.</li>
<li>Create a GnuPG signing key in /var/local/oab/gpg if none exists.</li>
<li>Sign the local apt repository using the local GnuPG signing key.</li>
</ul>
<span style="text-align: -webkit-auto;">What if this script didn't go well with you ? on of the problems you may face is throwing an error saying </span><b style="text-align: -webkit-auto;">"Error: Package sun-java6-jdk is not available, but is referred to by another package"</b><div style="text-align: -webkit-auto;">
<b><br /></b>You can run the following in the terminal :<div class="MsoPlainText" style="direction: ltr; unicode-bidi: embed;">
</div>
<div class="MsoPlainText" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New';"><br /></span></div>
<div class="MsoPlainText" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New';">sudo add-apt-repository ppa:ferramroberto/java<br />sudo apt-get update<o:p></o:p></span></div>
<span style="font-family: 'Courier New'; font-size: 10.5pt; line-height: 16px;">sudo apt-get install sun-java6-jdk</span><br />
<div class="MsoPlainText" style="direction: ltr; unicode-bidi: embed;">
<span style="font-family: 'Courier New';"><span style="font-size: 14px; line-height: 16px;"><br /></span></span></div>
By previous steps, it is expected that you have installed JDK 1.6 on your systems. The only remaining step is to set the class path of Java by the following commands:</div>
<div style="text-align: -webkit-auto;">
<br /></div>
<div style="text-align: -webkit-auto;">
<span style="font-family: 'Courier New'; font-size: 10.5pt; line-height: 16px;">export JAVA_HOME=/usr/lib/jvm/java-6-sun</span></div>
<div style="text-align: -webkit-auto;">
<span style="font-family: 'Courier New';"><span style="font-size: 14px; line-height: 16px;"><br /></span></span></div>
<div style="text-align: -webkit-auto;">
<span style="font-family: 'Courier New';"><span style="font-size: 14px; line-height: 16px;">To ensure that the new Java Home is set correctly, you can print (echo) the JAVA_HOME environment variable by the following command:</span></span></div>
<div style="text-align: -webkit-auto;">
<span style="font-family: 'Courier New';"><span style="font-size: 14px; line-height: 16px;"><br /></span></span></div>
<div style="text-align: -webkit-auto;">
<span style="font-family: 'Courier New'; font-size: 10.5pt; line-height: 16px;">echo $JAVA_HOME</span></div>
</div>Anonymoushttp://www.blogger.com/profile/01232893302882499506noreply@blogger.com1