Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Dec 9, 2013

JAVA Production Systems Profiling Done Right!

If you are facing a Java system performance issue in production, and JProfiler is not the right tool for it, probably JMX monitoring using the VisualVM will do the work for you.

Technical
JMX usage from a remote machine can be frustrating. Therefore, please make sure that:
  1. Your hostname is included in the /etc/hosts 
    1. Get host name using hostname 
    2. Add the host name after 127.0.0.1 in /etc/hosts
  2. JMX is binded to the external IP:
    1. Verify 127.0.0.1 is not presented at: netstat -na | grep 1099
    2. If it does presented, add to your java command: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=
If everything is Okay, you will be able to run VisualVM from a remote machine and connect to the remote server.

VisualVM
Now, that you have your VisualVM up and running there are some items you should take a look at:
  1. General CPU and memory graphs.
  2. Sampler that enables you taking snapshots.
  3. Snapshot analysis that enables you a hotspot presentation as well as deep.
Bottom Lin
My recommendation is to have snapshot of the process and then look at the hotspots tab for major calls with actual long CPU time. You should focus on these items.

Keep Performing,

Jun 25, 2013

MongoDB and Java

You can find below some hints for initial Java and MongoDB integration

Take a Look at the Requirements
  1. MongoDB
  2. MongoDB-Java-Driver
  3. JDK. If you want use JDK 1.6 or newer, you will get an error like this one: "DBObject cannot be resolved to a type"
Installing Java
yum -y install java-1.7.0-openjdk
yum -y install java-1.7.0-openjdk-devel

Getting MongoDB driver

Compiling Java
javac -d . *.java
java -cp . com/example/mbeans/Main

Define MongoDB Headers in the Code
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import com.mongodb.*;

Connect to MongoDB
DB _db;

public void init() {
try {
System.out.println("Connecting to mongo...");
MongoClient mongoClient = new MongoClient("127.0.0.1" , 27017);
_db = mongoClient.getDB("display");
System.out.println("Connected to mongo...");
} catch (Exception e) {
System.out.println("Failed Connecting Mongo...");
}

Query the Database (Get the Number of Connections)
CommandResult stats = _db.command("serverStatus");
return Integer.valueOf((((DBObject)(stats.get("connections"))).get("current")).toString());

Bottom Line
Java and MongoDB integration is not too difficult, you just need to do the right thinks right...

Keep Performing,

Jan 24, 2009

Java, MySQL and Large Datasets Retrieval

Hi,

As told before, it was a MySQL week,

We had a major work this week solving a performance issue in a reporting component to one of our clients. Since its current component worked directly against the raw database, it was facing degragated performance as database and business get larger.

Therefore, we designed an OLAP solution that extracts information from the raw tables, group and summarizes the data and then created a compact table, which data can be easily read from.

However, the database is MySQL, and we used Java to implement this mechanism. Unfortunately, it seems that Java and MySQL don't really each other or at least like large tables: When you try to extract records our of large MySQL table you receive out of memory error in the execute and executeQuery methods.

How to overcome this?
1. As suggested by databases&life, set the fetchSize to Integer.MIN_VALUE. Yes, I know it a bug, not a feature, but yet it solves this issue:

The reason for this bug is the code in StatementImpl.java of the MySQL JDBC driver code:
protected boolean createStreamingResultSet() {
return ((resultSetType == ResultSet.TYPE_FORWARD_ONLY)
&& (resultSetConcurrency == ResultSet.CONCUR_READ_ONLY)
&& (fetchSize == Integer.MIN_VALUE));
}

And the solution is:

public void processBigTable() {
PreparedStatement stat = c.prepareStatement(
"SELECT * FROM big_table",
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY
);
stat.setFetchSize(Integer.MIN_VALUE);

ResultSet results = stat.executeQuery();

while (results.next()) {
...
}
}


2. The other option is doing this fetch applicative, meaning that each time setMaxRows will set to N and reocrds will be extracted only if their id is larger than the extracted before

public void processBigTable() {
long nRowsNumber = 1;
long nId = 0;
while (nRowsNumber > 0) {
nRowsNumber = 0;
PreparedStatement stat = c.prepareStatement(
"SELECT * FROM big_table WHERE big_table_id > " + nId ,
ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY
);
stat.
setMaxRows(10000);
ResultSet results = stat.executeQuery();

while (results.next()) {
++nRowsNumber;
nId = results.getLong(1);
...
}
}

Hope you find it useful as we found it, and thanks again to databases&life,

Best
Moshe. RockeTier. The Performance Experts.

Dec 8, 2008

Top Performance JVM and Cloud Computing

Hi,
In the last three months we at RockeTier help a Java based SaaS company boost their software system performance and achieve their next success story.
Java? Can it be boosted?
Java is a byte code compiled language. Therefore, it usually drags several performance limitations compared to fully compiled code (like unmanaged C++).
One of first recommendations in this project was JVM migration to JRockit. BEA JRockit a JVM optimized for the Intel platform, enabling Java applications to run with increased reliability and performance on lower cost, standard-based platforms. According to industry benchmarks (here and here), BEA JRockit leads in performance over other published RISC-based benchmark results. BEA mentioned a boost ratio of 50% to 100%, These is definitely the numbers we are familiar of.
How JRockit achieve these results?
Well they are using several smart techniques out there:
  • Optimized Code Generation - They monitor continuously the code, and adapting the JIT compilation according to the dynamic code, rather than using known general statistics. They are using a background bottleneck detector as well, that collects runtime statistics to detect bottlenecks caused by frequently executed methods. These statistics are being used even on run time to deliver on-the-spot improvements
  • Seamless Garbage Collection - They eliminate the pauses and operational disruption that garbage collection often causes. They are doing that in several ways in order to meet different types of applications and environments (more details can be found here).
Why is it relevant to Cloud Computing?
x86 CPUs are the de facto standard in Cloud Computing. Therefore, JVM that better utilize the cloud computer CPU, requires less CPU hours to complete a given task and therefore saves you money every hour. Therefore, using optimized JVM (and software) help you reduce your Op-EX when you choose cloud computing.
And the bottom line?
Surprise, you can download the JRockit here, absolutely free out of charge...
I think there is no doubt in here; it is a call for action.
Start up your engines :-)
Moshe,

ShareThis

Intense Debate Comments

Ratings and Recommendations