skip to main | skip to sidebar

Monday, July 18, 2011

JVM Heap Fragmentation

Recently I read about this topic and wanted to share it, I couldn’t recollect the source where I read it first to give a reference link, so just writing it here.
When JVM requires memory, Operating System always allocates continuous memory blocks. When objects get created and destroyed frequently, JVM allocates memory where ever available. Think of a situation where 1000 [each of size 5bytes] objects got allocated in some ‘x’ time span and only 500 of them are killed/de-allocated. Memory occupied by these 500 objects might not be continuous, the de-allocated memory might me scattered across heap memory. Each de-allocated block is of size 5bytes – so, you might be having free memory block of size 5bytes across heap, if JVM wants to allocate a 10byte memory block it cannot use these 5bytes blocks, it has to find a continuous 10byte free space in heap and allocate it. This situation is called as "Heap Fragmentation" [same as ‘Disk Fragmentation’], where the heap memory is fragmented. This results JVM not to release the continuous memory to Operating System.
Well, that is about the problem – how can we avoid it, here it is.
  • Identify places and avoid them where the objects are unnecessarily getting created and destroyed frequently.
  • Keep the maximum Heap memory Size setting to as close as possible to the maximum memory required by your JVM.
  • You can compact the Heap memory, using the JVM option "-compactGc", but remember that this option might result performance reduction.
That is all for now :)

Friday, July 15, 2011

Ant Script to make Executable JAR

Recently I observed search patterns about Ant script to make executable JAR files, many end up on wrong pages of my blog. This blog post is for those who are searching for it. Its fairly easy and simple to use Ant Script, to make an executable JAR file, see the script below.
<jar destfile="_dist/my-executable-jar.jar">
    <fileset dir="_bin" />
    …     <manifest>         <attribute name="Main-Class" value="com.mypackage.ClassWithMain" />     </manifest>
As you can see, we usee the ‘jar’ ant task to create a JAR file, we want to create the JAR file with the name ‘my-executable-jar.jar’ – which is given as ‘destfile’ attribute value of ‘jar’ task and we wanted to include all the compiled classes which are available at ‘_bin’ directory – which is provided using ‘fileset’ tag in the second line of the script.
Then comes the important part – the ‘manifest’ tag. If you want to make a JAR file executable – you’ve to add a ‘Main-Class’ attribute to the MANIFEST file of your JAR file. As we wanted to specify an attribute of MANIFEST file – that is why the ‘manifest’ tag. In this tag we’ve a ‘attribute’ tag with ‘name’ and ‘value’ tag attributes. ‘name’ attribute says which attribute of the MANIFEST file you want to set and with which value, value is provided using ‘value’ attribute. In the above sample script we mentioned ‘Main-Class’ as attribute name and the value for this attribute as ‘com.mypackage.ClassWithMain’ – by this we are saying that the class which need to be used when we execute the JAR file is ‘com.mypackage.ClassWithMain’ [which means this class has the main() method and is the starting point for execution].
That is all – Happy Scripting :)

Wednesday, July 13, 2011

JAR Dependency and Ant Jar Task [Merging Archives]

Ever had a situation where you are using Ant to build a JAR file and this JAR uses classes from other JAR OR needs few other JAR files to be in class path. Placing dependent JARs in class path manually is one way of doing it, but what if you want to generate/deliver/use only one JAR [may be executable] which includes all the dependent JARs in it. ‘zipfileset’ attribute of the ‘jar’ ANT task is the one to use.
Say, you are trying to generate a JAR file with name ‘my-application.jar’, which needs/depends on another JAR with the name ‘some-dependency.jar’. Assuming all the compiled classes are available under ‘_bin’ directory, the dependent JAR is available under ‘lib’ directory and you want to place the generated JAR under ‘_dist’ directory, ANT script might look as given below
<jar destfile="_dist/my-application.jar">
    <fileset dir="_bin" />
    <zipfileset src="lib/some-dependency.jar" includes="**/*.class" excludes="META-INF/**/*" />
That is all you need, only extra line you’ve is ‘zipfileset’ attribute. This attribute takes the JAR file [some-dependency.jar] to be added to the target JAR. You can mention what files from this JAR need to included OR excluded using the ‘includes’ and ‘excludes’ attributes. The above code says to include all ‘.class’ files and exclude all files under ‘META-INF’ directory.
What if you have more than one JAR to include, say all the JAR files under ‘lib’ directory, one way is to add multiple ‘zipfileset’ tag for each of those JAR files, if you’ve a big list of JARs under your ‘lib’ directory then, you can use the ANT script given below
<unjar dest="_unjar" overwrite="false">
    <fileset dir="lib" includes="**/*.jar"/>
<jar destfile="_dist/my-application.jar">     <fileset dir="_bin" />     <fileset dir="_unjar" includes="**/*.class"/> </jar>
What we did here is, we un-jared all the JAR files under ‘lib’ directory to a temporary directory ‘_unjar’, and include all the ‘.class’ files using the ‘fileset’ tag, simple isn’t it!