As a college student in a famous university of China, I am participated in National Key Laboratory to develop a mobile multimedia system. In this project, Red5, an open source flash server written in Java, is chosen as streaming server. As a Sun Certificated Java Programmer, I am responsible for developing Java components to extend the functions Red5 currently provides. In this article, I will share some experience about how VisualVM facilitates Java development and makes the developing of complex projects an easier job.
The system architecture is showed above. It models a client/server-based multimedia system. The central server is responsible for registration, authentication, multimedia storage and live stream publishing. Considering all the media in user’s computer is not in FLV format, each received media have to be processed before being published by Red5. As our system enables live stream publishing, the delay of uploading, processing and downloading have to be minimized. My job is developing a Java program to optimize this procedure. In the process of developing an optimization program, VisualVM plays a very important role.
VisualVM Installation
Installation was as easy as usual, the version I use is JDK6u14; however, when started for the fist time, the VisualVM warned that Local Application Cannot Be Detected. Though website is provided to solve the problem, https://visualvm.dev.java.net/ is not accessible in China. I searched other Java forums to solve this problem and make VisualVM run.
Find the Bottleneck
Firstly, I tested the video processing component in local computer. The GUI of video processing component is illustrated as follows
When the media is processing, it is using 49.5%percent of my 1.59GHz Sempron, which is showed in the Figure below. As performance is the major concern of my job, I used tools provided by VisualVM to find out where the problem lies. In the beginning, I select a short piece of film in AVI format to process. It took 20 minutes to convert a 10M video and this speed, of course, was unbearable. Even worse, when the conversion was finished, some threads may still keep the references of the original video, which make the component can not successfully exit.
When I opened the snapshot view I find out how much time each method consumed. Fortunately, I get explicit information from VisualVM and get to know where to optimize. In our code, there is a sort() function, it was called 2867 times and occupied 48.9% of time. I scrutinized the code and only to find that the sort () function was realized in bubble sort!! So I changed to use Java API, which largely improved the performance.
Secondly, the transmission component was always down. Sometimes it uploaded files that were damaged and sometimes it keeps running for no reason. In order to solve this subtle problem, I used VisualVM again to what was happening inside the component. In this component, a thread pool was used to enable the access of more than one user. Through VisualVM, I found that the number of threads was consistently increasing rather than kept stable. Then I checked the code about the thread pool and found that a function was not written correctly and resulted in creating a new thread each time the users requested. The threads is showed is the Figure below. When the error is fixed, my compoent can run smoothly.
Conclusion
VisulaVM is really a useful tool for troubleshooting. Personally I think it is easier to use than JProfiler. I am a SCJP who have passion for the latest technology developed by Sun. VisualVM is one of Sun’s 27 years innovations and it shows the brilliance of Sun’s engineers.