Tuesday, November 27, 2007

Setting the class path in UNIX

CLASSPATH=classpath1:classpath2...

Multiple path entries are separated by colons. Classpath can be either archive files or folder.To include all the Jar files in a folder/directory, include the following script to collect all the jar files from the folder and will be added to the classpath.


CP=
for i in `ls $OH/j2ee/lib/*.jar `
do
 CP=$CP:$i
done
java -cp CP yourclass

Setting the class path in Windows

set CLASSPATH=classpath1;classpath2...

Classpath entries that are neither directories nor archives (.zip or .jar files) are ignored. If the classpath ends with a file location that should be archives. If the classpath ends with the directory then the *.class found in the path will be added in JVM CLASSPATH

Is there a way to add all the jar files in a folder / directory to CLASSPATH?.

No. There is no way of setting directly the all the jars in folder to the classpath.

set CLASSPATH=%CLASSPATH%;%LIB%/java/classes.jar
set CLASSPATH=%CLASSPATH%;%LIB%/java/rt.jar
set CLASSPATH=%CLASSPATH%;%LIB%/java/junit.jar
set CLASSPATH=%CLASSPATH%;%LIB%/java/tools.jar

Alternate approach is possible by collecting the jars from a folder and setting into CLASSPATH.

set CP=
for %%f in (%LIB%\*.jar) do set CP=!CP!;%%f
echo %CP%
java -cp %CP% yourclassname

Keep in mind:

Here ! means that variables values are expanded later.

Don't Use CLASSPATH instead of CP because it will lead you to expose these setting to environment. If you want to use CLASSPATH then the code has to be placed within setlocal and endlocal.

Delayed environment variable expansion support is always disabled by default, but may be enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?

The above mentioned echo %CP% will display as !CP!;%LIB%/java/tools.jar , but this will be expanded while using in any commands like java, javac.Show text-ads on your Website or Blog with BidVertiser.

Few of Dynamic variables from Windows:

If Command Extensions are enabled, then there are several dynamic environment variables that can be expanded but which don't show up in the list of variables displayed by SET. These variable values are computed dynamically each time the value of the variable is expanded. If the user explicitly defines a variable with one of these names, then that definition will override the dynamic one described below:

%CD%
expands to the current directory string.
%DATE%
expands to current date using same format as DATE command.
%TIME%
expands to current time using same format as TIME command.
%RANDOM%
expands to a random decimal number between 0 and 32767.
%ERRORLEVEL%
expands to the current ERRORLEVEL value
%CMDEXTVERSION%
expands to the current Command Processor Extensions version number.
%CMDCMDLINE%
expands to the original command line that invoked the Command Processor.

Java Launcher Finds Classes

The virtual machine searches for and loads classes in this order:

Bootstrap classes
Classes that comprise the Java platform, including the classes in rt.jar and several other important jar files.
Extension classes
Classes that use the Java Extension mechanism. These are bundled as jar files located in the extensions directory.
User classes
Classes defined by developers and third parties that do not take advantage of the extension mechanism. You identify the location of these classes using the -classpath option on the command line (the preferred method) or by using the CLASSPATH environment variable.

Sunday, November 18, 2007

Default Heap Size

If not otherwise set on the command line, the sizes of the initial heap and maximum heap are calculated based on the size of the physical memory. If phys_mem is the size of the physical memory on the platform, the initial heap size will be set to phys_mem / DefaultInitialRAMFraction.

DefaultInitialRAMFraction is a command line option with a default value of 64. Similarly the maximum heap size will be set to phys_mem / DefaultMaxRAM.

DefaultMaxRAMFraction has a default value of 4.

About Generational GC
JVM heap size
Heap Size
Heap Size Options
Heap Size
OutOfMemoryExeception

OutOfMemoryExceptions

The throughput collector will throw an out-of-memory exception if too much time is being spent doing garbage collection.

For example, if the JVM is spending more than 98% of the total time doing garbage collection and is recovering less than 2% of the heap, it will throw an out-of-memory expection. The implementation of this feature has changed in 1.5. The policy is the same but there may be slight differences in behavior due to the new implementation.

About Generational GC
JVM heap size
Heap Size
Heap Size Options
Heap Size
OutOfMemoryExeception

Thursday, November 15, 2007

About Generational Garbage Collection

The Java HotSpot JVM 1.3 uses generational garbage collection. While naive garbage collection examines every living object in the heap, generational garbage collection considers the lifetime of an object to avoid extra work.

The heap is divided into two general areas: New and Old. The New generation area is sub-divided further into Eden and two survivor spaces. Eden is the area where new objects are allocated. When garbage collection occurs, live objects in Eden are copied into the next survivor space. Objects are copied between survivor spaces in this way until they exceed a maximum threshold, and then they are moved out of the New area and into the Old.

Many objects become available for garbage shortly after being allocated. These objects are said to have infant mortality. The longer an object survives, the more garbage collection it goes through, and the slower garbage collection becomes. The rate at which your application creates and releases objects determines how often garbage collection occurs. Attempt to cache objects for re-use, whenever possible, rather than creating new objects

Knowing that a majority of objects die young allows you to tune for efficient garbage collection. When you manage memory in generations, you create memory pools to hold objects of different ages. Garbage collection can occur in each generation when it fills up. If you can arrange for most of your objects to survive less than one collection, garbage collection is very efficient. Poorly sized generations cause frequent garbage collection, impacting your performance.

About Generational GC
JVM heap size
Heap Size
Heap Size Options
Heap Size
OutOfMemoryExeception

About JVM Heap Size

Garbage collection is the VM process of de-allocating unused Java objects in the Java heap.The Java heap is where the objects of a Java program live. It is a repository for live objects, dead objects, and free memory. When an object can no longer be reached from any pointer in the running program, the object is garbage.

The JVM heap size determines how often and how long the VM spends collecting garbage. An acceptable rate for garbage collection is application-specific and should be adjusted after analyzing the actual time and frequency of garbage collections.

If you set a large heap size, full garbage collection is slower, but it occurs less frequently. If you set your heap size in accordance with your memory needs, full garbage collection is faster, but occurs more frequently.p>

The goal of tuning your heap size is to minimize the time that you spend doing garbage collection while maximizing the number of clients that you can handle at a given time.

To ensure maximum performance during benchmarking, you might set high heap size values to ensure that garbage collection does not occur during the entire run of the benchmark.

You might see the following java error if you are running out of heap space:

java.lang.OutOfMemoryError 
java.lang.OutOfMemoryError Exception in thread "main"
About Generational GC
JVM heap size
Heap Size
Heap Size Options
Heap Size
OutOfMemoryExeception

Specifying Heap Size Values

java ... -XX:NewSize=128m -XX:MaxNewSize=128m -XX:SurvivorRatio=8
-Xms512m -Xmx512m 

The default size for these values is measured in bytes. Append the letter `k' or `K' to the value to indicate kilobytes, `m' or `M' to indicate megabytes, and `g' or `G' to indicate gigabytes.

About Generational GC
JVM heap size
Heap Size
Heap Size Options
Heap Size
OutOfMemoryExeception

Java Heap Size Options

Setting the New generation heap size

-XX:NewSize

Use this option to set the New generation Java heap size. Set this value to a multiple of 1024 that is greater than 1MB. As a general rule, set -XX:NewSize to be one-fourth the size of the maximum heap size. Increase the value of this option for larger numbers of short-lived objects.

Be sure to increase the New generation as you increase the number of processors. Memory allocation can be parallel, but garbage collection is not parallel.

Setting the maximum New generation heap size

-XX:MaxNewSize

Use this option to set the maximum New generation Java heap size. Set this value to a multiple of 1024 that is greater than 1MB.

Setting New heap size ratios

-XX:SurvivorRatio

The New generation area is divided into three sub-areas: Eden, and two survivor spaces that are equal in size.

Use the -XX:SurvivorRatio=X option to configure the ratio of the Eden/survivor space size. Try setting this value to 8 and then monitor your garbage collection.

Setting minimum heap size

-Xms

Use this option to set the minimum size of the memory allocation pool. Set this value to a multiple of 1024 that is greater than 1MB. As a general rule, set minimum heap size -Xms equal to the maximum heap size -Xmx.

Setting maximum heap size

-Xmx

Use this option to set the maximum Java heap size. Set this value to a multiple of 1024 that is greater than 1MB.

About Generational GC
JVM heap size
Heap Size
Heap Size Options
Heap Size
OutOfMemoryExeception

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More