Exforsys.com
 
Home Tutorials J2EE
 

How a Profiler Can Improve Your Java Applications

 

How a Profiler Can Improve Your Java Applications

If you have been working with C++ programs for longer than five years, you should be well aware of the difficulties involved with debugging software. However, many younger programmers don't have experience with these issues because Java deals with memory problems by using what is called the garbage collector. Whenever a new object is created in Java, the Java Virtual Machine will reserve memory for it, and if the object reference disappears, the memory will be reclaimed.



Because of this simple tool, your small to medium sized Java programs will not crash due to memory issues. When it does fail due to memory issues, many Java programmers have problems. This is where a Java profiler can help you. A Java profiler is a system which connects to a Java program's JVM. Once it has made this connection, it will capture information about the memory. The profiler uses a special interface which works with the Java Virtual Machine. If you don't set a profile for your large Java programs before you release them to be manufactured, they may have a number of OutOfMemoryErrors. They may also have performance problems.

JVMPI stands for Java Virtual Machine Profiler interface, and it was a project that worked with Java 2 SDK. Sun wanted it to be used to create profilers which could effectively work with the Java Virtual Machine. Profiling instruments that worked with JVMPI had to utilize the function call interface. This would allow them to register for various situations where they need to get memory information. When a register occurs, the VM will collect a snapshot of the memory by running a query on the object. This process took up a large amount of time, and it also interfered with the operations of the Java program. Because of this, many developers tried to avoid designing profiling tools.

To solve these problems, Sun created JVMTI along with JDK 5.0. The goal was to create an interface which could effectively profile the Java Virtual Machine. It created ways to both study the state and handling of programs running within the JVM. It also supports a wide variety of tools that need to have access to the JVM. Once these tools are given access, they are able to monitor, profile, and debug the system. In addition to this, the JVMTI supports sampling for JVM as well. JVMTI is composed of two parts, and this is the control and query. A client of this tool can be informed of situations via events. By using JVMTI, a profiler can query and handle the program through a large number of different functions. Some of these functions are GetLoadedClasses and GetEnv.

There are two types of memory which is used by the Java Virtual Machine, and these are old generation space and young generation space. The old generation space will store objects which have existed for quite some time. The young storage space will store elements which have recently been generated. The young generation space is further divided in three categories. Two of these spaces are called survivor spaces, while the third is called the Eden space. The Java Virtual Machine will place objects within the Eden space. Many of these objects will be destroyed and reclaimed quickly. Once the Eden space becomes filled, it will cause the JVM to do a small collection. It will move some of the objects which weren't destroyed to the old generation.

There are two survivor spaces available which will allow you to copy objects, and this will allow young objects to stay with the younger generation for a longer period of time. There will be one survivor space opened at all times.

If the objects become old, or the space for the young generation becomes full, the Java Virtual Machine will begin placing objects in the old generation space. Once this space becomes full, the Java Virtual Machine will begin a collection so that objects which are not being used can be removed. A large collection can take a long period of time and may slow down the system. Because of this, it is important to make sure that large collections do not consistently occur within their programs. Altering the object variables can reduce the pressure that has been placed on the Java Virtual Machine.



Read Next: How Code Reviews and PMD Can Crush Bugs In Your Code



 

 

Comments


mikew12345 said:

  This is interesting material from the 10,000 foot level. It could use some more down-in-the-dirt emphasis, with guidance on how to run the tools in JDeveloper, what to look for, etc. Even basic guidance such as "If you 'new" something, set it to null when you're done with it so GC can reclaim it" would be helpful. The JDeveloper profiler has some really cool capabilities that are worth mentioning, such as being able to drill down on any object in memory to see the stack that used it, including the final one that must release it before GC can reclaim it.
July 16, 2008, 5:05 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape