Java: Bin/Dec/Hex Conversion and Primitive Data Types

March 19th, 2010 No comments

Conversion Table

HEX DECIMAL BINARY
0 0 = 0+0+0+0 0000
1 1 = 0+0+0+1 0001
2 2 = 0+0+2+0 0010
3 3 = 0+0+2+1 0011
4 4 = 0+2+0+0  0100
5 5 = 0+4+0+1 0101
6 6 = 0+4+2+0 0110
7 7 = 0+4+2+1 0111
8 8 = 8+0+0+0 1000
9 9 = 8+0+0+1 1001
A 10 = 8+0+2+0 1010
B 11 = 8+0+0+1 1011
C 12 = 8+4+0+0 1100
D 13 = 8+4+0+1 1101
E 14 = 8+4+2+0 1110
F 15 = 8+4+2+1  1111

Data Types and Data Structures

Primitive Type Size Minimum Value Maximum Value
char 16-bit Unicode 0 Unicode 216-1
byte 8-bit -128 +127
short 16-bit -215
(-32,768)
+215-1
(32,767)
int 32-bit -231
(-2,147,483,648)
+231-1
(2,147,483,647)
long 64-bit -263
(-9,223,372,036,854,775,808)
+263-1
(9,223,372,036,854,775,807)
float 32-bit 32-bit IEEE 754 floating-point numbers
double 64-bit 64-bit IEEE 754 floating-point numbers
boolean 1-bit true or false
void —– —– Void

Create and Apply Patch in SVN

March 16th, 2010 No comments

Creating a patch file

Creating a patch file is really easy. First, check out the most recent version of the code from Subversion using the ‘checkout’ command.

Make your changes. Then, in the root the project run the following command. It will store the patch file in your home directory. Make sure to give it meaningful filename.

svn diff > ~/fix_bug.diff

The file has the .diff extention, which stands for differences. This extension is recognized by many text editors and enables ’syntax highlighting’ automatically. (Give it a try with TextMate and you’ll know what I mean.) You can send the diff-file to the author of the project by email, or you can create a ticket in Trac and add it as an attachment. The author will review the changes you made and possibly apply them to the source.

Applying a patch

You should never apply patches from any person other than your development team without first reading through the changes, apply them locally and test your application and then commit them. Patches can not only include bug fixes, but also alterations to create back doors or add other exploits to your code.

Always read through a patch before applying it!

When you are sure the patch will bring no harm to you, your application or your customers, go ahead an apply it to your working copy. Here, I assume that you downloaded the patch file we previously generated, and placed it in your home directory. In the root of your application now run:

patch -p0 -i ~/fix_bug.diff

This will apply all the changes in the patch to your source. The -p0 option makes sure that all files can be found correctly (this has to do with something called ‘zero directories’, I won’t get into that right now). The -i option tells ‘patch’ what to use as input, in this case the ‘fix_bug.diff’ file in your home directory.
With the code changes in place, run your tests and make sure everything works as expected. If it does, commit your changes.

JProfiler: Getting Started

February 4th, 2010 No comments

JProfiler helps you find performance bottlenecks, pin down memory leaks and resolve threading issues in your Java application. JProfiler combines CPU, Memory and Thread profiling in one application and is developed by ej-technologies. The latest version at the time of this article is 6.0.

MySQL 5.5 and Semisynchronous Replication Available

December 15th, 2009 No comments

The new 5.5 is now available with “Semisynchronous Replication”. This comes as an addition to the built-in asynchronous replication. MySQL replication is asynchronous by default. Events written to the binary logs on the Master server being retrieved by the slave server(s). Unfortunately, the Master server has no knowledge of when the slave has retrieved or processed these events. As a result, when Master crashes, transactions committed on Master, might have not have been committed on the slave server(s). In other words, there is no guarantee that any event will ever reach any slave.

In case of “Semisynchronous Replication”, the Master server blockes the transaction commit until at least one of the slave servers acknowledges the it has received all the events for that transaction. Obviously, this is great for consistency, however it brings up questions about replication over the WWW which isn’t an uncommon practice. In case of hitting the timeout limit (in case of no response from any of the slave servers), the Master server reverts back to asynchronous replication.

From MySQL support:

To understand what the “semi” in “semisynchronous replication” means, compare it with asynchronous and fully synchronous replication:

* With asynchronous replication, the master writes events to its binary log and slaves request them when they are ready. There is no guarantee that any event will ever reach any slave.
* With fully synchronous replication, when a master commits a transaction, all slaves also will have committed the transaction before the master returns to the session that performed the transaction. The drawback of this is that there might be a lot of delay to complete a transaction.
* Semisynchronous replication falls between asynchronous and fully synchronous replication. The master waits after commit only until at least one slave has received and logged the events. It does not wait for all slaves to acknowledge receipt, and it requires only receipt, not that the events have been fully executed and committed on the slave side.

Humor: Software Development Cycle

July 28th, 2009 No comments

Software doesn’t just appear on the shelves by magic. That program shrink-wrapped inside the box along with the indecipherable manual and 12-paragraph disclaimer notice actually came to you by way of an elaborate path, through the most rigid quality control on the planet. Here, shared for the first time with the general public, are the inside details of the program development cycle.

1. Programmer produces code he believes is bug-free.
2. Product is tested. 20 bugs are found.
3. Programmer fixes 10 of the bugs and explains to the testing department that the other 10 aren’t really bugs.
4. Testing department finds that five of the fixes didn’t work and discovers 15 new bugs.
5. See 3.
6. See 4.
7. See 5.
8. See 6.
9. See 7.
10. See 8.
11. Due to marketing pressure and an extremely pre-mature product announcement based on overly-optimistic programming schedule, the product is released.
12. Users find 137 new bugs.
13. Original programmer, having cashed his royalty check, is nowhere to be found.
14. Newly-assembled programming team fixes almost all of the 137 bugs, but introduce 456 new ones.
15. Original programmer sends underpaid testing department a postcard from Fiji. Entire testing department quits.
16. Company is bought in a hostile takeover by competitor using profits from their latest release, which had 783 bugs.
17. New CEO is brought in by board of directors. He hires programmer to redo program from scratch.
18. Programmer produces code he believes is bug-free….

Java: GC Options

June 24th, 2009 No comments

Verbose GC options

Option Default value Max Value Description
-XX:+PrintGCDetails false PrintGC details
-XX:+PrintGCTimeStamps false Adds timestamp info to GC details
-XX:+PrintHeapAtGC false Prints detailed GC info including heap occupancy before and after GC
-XX:+PrintTenuringDistribution false Prints object aging or tenuring information
-XX:+PrintHeapUsageOverTime false Print heap usage and capacity with timestamps
-Xloggc:filename false Prints GC info to a log file
-verbose:gc false Prints some GC info
-XX:+PrintTLAB false Print TLAB information

Read more…

千の風になって

June 7th, 2009 No comments

Profiling MySQL Queries

May 23rd, 2009 No comments

Profiling Queries with SHOW STATUS
mysql> flush status;
mysql> select SQL_NO_CACHE count(*) from table;

- Check query plan now:

mysql> show status like 'Select%';

- Check engine operations:

mysql> show status like 'Handler%';

- Check if there was any ordering:

mysql> show status like 'Sort%';

- Check how many temporary tables have been created:

mysql> show status like 'Created%';

Read more…

Find the Biggest CPU Hog

March 27th, 2009 No comments

Is a certain process running your CPU right into the ground? How do you find said process without picking your way through the ps aux results? With this command:

ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu | sed '/^ 0.0 /d'

…at which point you can kill it with sudo kill -9.

Start MySQL with New Binaries and Other Misc Things

February 28th, 2009 No comments
1
2
./mysqld_safe --user=mysql --basedir=/usr/local/mysql-5.0.67-linux-x86_64-icc-glibc23
--ledir=/usr/local/mysql-5.0.67-linux-x86_64-icc-glibc23/bin --mysqld=mysqld
1
2
./mysqladmin ext -u root -p -ri60
./mysqladmin ext -u root -p -ri60 | grep tmp