ThreadLocal in Java

September 15, 2009

ThreadLocal is one of the rarely used class in Java. I found it is one of the powerful class in Java on multi-threaded programming. In multi threaded program generally we use session object to track the current users information.

These information is passed to various method to retrieve desired value. For example in Struts execute method passes HttpServletRequest and HttpServletResponse, what if we want the instance of ServletContext? we have to change the method signatore to pass ServletContext. One can use ThreadLocal to keep certain objects/values available throught the thread execution.

ThreadLocal object is not required on day to day programming unlike ArrayList or HashMap. But it is good choice to solve a few problems at framework level. In one of my recent project I have used ThreadLocal to expose user specific information like User info, UserId, Roles and access, etc.

The main reason is I want hide the code which identifies these values so that in future it is easy to change the behavior and fine tune the code across the application.


Out of memory error:java heap space

August 6, 2008

Hi guys,

 

 We are most struggling with “Out of memory error” in web application when reference memory is over head.

 

For example:

           

            public final class Large { 

private static final double[ ] [ ] [ ] matrix = new double [1357] [ 1687] [4]; 

                        public static final void main(String[] arg) {

                        }

            }

 

O/P:

java.lang.OutOfMemoryError: Java heap space

                        at outofmemory.Large.<clinit>(Large.java:5)

Exception in thread “main”

 

 

How to solve?

 

            Increase java heap space based on jvm which you used.

 

Set java heap size in JVM?

java -Xms64m -Xmx512m

 

where -Xms<size> specifies the initial Java heap size and -Xmx<size> the maximum Java heap size. 

 

 

Set java heap size in Tomcat?
Stop Tomcat server, set environment variable
CATALINA_OPTS, and then restart Tomcat. Look at the file tomcat-install/bin/catalina.sh or catalina.bat for how this variable is used. For example,

set CATALINA_OPTS="-Xms512m -Xmx512m"  (Windows)
export CATALINA_OPTS="-Xms512m -Xmx512m"  (ksh/bash)
setenv CATALINA_OPTS "-Xms512m -Xmx512m"  (tcsh/csh)

In catalina.bat or catallina.sh, you may have noticed CATALINA_OPTS, JAVA_OPTS, or both can be used to specify Tomcat JVM options. What is the difference between CATALINA_OPTS and JAVA_OPTS? The name CATALINA_OPTS is specific for Tomcat servlet container, whereas JAVA_OPTS may be used by other java applications (e.g., JBoss). Since environment variables are shared by all applications, we don’t want Tomcat to inadvertently pick up the JVM options intended for other apps. I prefer to use CATALINA_OPTS.
 

 

Set java heap size in JBoss?
Stop JBoss server, edit
$JBOSS_HOME/bin/run.conf, and then restart JBoss server. You can change the line with JAVA_OPTS to something like:

JAVA_OPTS="-server -Xms128m -Xmx128m"
 

 

 

 

Set java heap size in Eclipse?
You have 2 options:
1. Edit eclipse-home/eclipse.ini to be something like the following and restart Eclipse.

-vmargs
-Xms64m
-Xmx256m

2. Or, you can just run eclipse command with additional options at the very end. Anything after -vmargs will be treated as JVM options and passed directly to the JVM. JVM options specified in the command line this way will always override those in eclipse.ini. For example,

eclipse -vmargs -Xms64m -Xmx256m

 

 

Set java heap size in Weblogic?

 

Java heap size values must be specified whenever you start WebLogic Server. This can be done either from the Java command line or by modifying the default values in the sample startup scripts that are provided with the WebLogic distribution for starting WebLogic Server.

For example, when starting WebLogic Server from a Java command line, the heap size values could be specified as follows:

$ java -XX:NewSize=128m -XX:MaxNewSize=128m -XX:SurvivorRatio=8  -Xms512m -Xmx512m
-Dweblogic.Name=%SERVER_NAME% -Dbea.home=”C:\bea”
-Dweblogic.management.username=%WLS_USER%
-Dweblogic.management.password=%WLS_PW%
-Dweblogic.management.server=%ADMIN_URL%
-Dweblogic.ProductionModeEnabled=%STARTMODE%
-Djava.security.policy=”%WL_HOME%\server\lib\weblogic.policy”
 weblogic.Server

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.

 

For more information on the heap size options

 

 

Setting the New generation heap size                        -XX:NewSize

Setting the maximum New generation heap size                   -XX:MaxNewSize

Setting New heap size ratios                                                  -XX:SurvivorRatio

Setting minimum heap size                                                     -Xms

Setting maximum heap size                                                    -Xmx