Can I pre-allocate certain amount of memory for Thread. Is thread able to using 2 processors power?

Hi,

I would like to know do we able to allocate certain amount of memory for particular thread in the program? If yes, how should i do it? Any example?

Is thread able to use more than 1 processor's processing power in a program? If yes, how should i do it? Any exmple?

FYI, anyone able to provide solutions/ examples will be getting additional points. I m willing to add on more points if the answer provided working perfectly. Extra points can be added up to 500 points.

Thanks a lot.
fionAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
threads aren't allocated memory individually
0
 
girionisConnect With a Mentor Commented:
AFAIK you can't do it in standard Java but you will probably be able to do it in a real-time implementation of the JVM. You could pre-allocate memory for certain objects but don't think you can do it for the whole thread.
0
 
fionAuthor Commented:
>You could pre-allocate memory for certain objects but don't think you can do it for the whole thread.

Any reference saying so?

how about the second question?

>Is thread able to use more than 1 processor's processing power in a program? If yes, how should i do it? Any example?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
How many processers are used I think is up to the OS, but could well be wrong.
0
 
girionisCommented:
> > You could pre-allocate memory for certain objects but don't think you can do it for the whole
> > thread.
>
> Any reference saying so?

You can do for example:

byte [] bytesArray = new byte[4096]

and this would pre-allocate 4096 bytes for the "bytesArray" object.

>Is thread able to use more than 1 processor's processing power in a program? If yes, how should i
>do it? Any example?

Not sure. Each processor has it's own cache and if a thread could use another processor's power then it could be writing to that cache. It might be possible though, I am not sure what exactly going on.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, it is the job of the OS to do resource-allocation, so I guess in a multi-proc system, it would be the OS which would decide which processor to use for which thread because the processor is nothing but like a resource.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
However, I feel personally that one thread would use only one processor. (girinois' point about the cache is also strong.) When you have multiple threads running in a process, they could well be assigned to different processors.
0
 
fionAuthor Commented:
>AFAIK you can't do it in standard Java but you will probably be able to do it in a real-time implementation of the JVM.

How about any reference that saying JVM able to allocate memory to thread or single thread is able to use more than 1 Processor available?

Hi, have a look at this link
http://forum.java.sun.com/thread.jsp?thread=3716&forum=1&message=9871
0
 
girionisCommented:
I am not sure about the standard java implementation since I believe it is not possible. Do a search for real-time implementation of JVM and you might find something. Do a search for "real time java" on google.
0
 
sciuriwareConnect With a Mentor Commented:
Pre-allocate? Yes you can; I was surprised that it worked and now I use it for huge applications:

javaw -Xms256m -Xmx512m <whatever you already typed>

Xmx tells JVM that you expect to grow to 512Mb and Xms orders for 256Mb allocation at startup.
It's even a bit faster!

The downside to this is that everywhere is stated that the -X options may disappear in the future.

.. and about the threads.... the boys before me were right.
If you want to know about the CPU-thread combination, write a little program sending 2 or 3 threads in
endless loops. Then monitor the OS: if you see 2 resp. 3 CPU's warm up ... there's your answer!

;JOOP!
0
 
girionisCommented:
The original question is to allocate memory to a "thread" not to the whole JVM :)
0
 
sciuriwareCommented:
fion may decide about this, and some other people did not know about -Xms ...

;JOOP!
0
 
fionAuthor Commented:
>javaw -Xms256m -Xmx512m <whatever you already typed

Can u give a clear example on this line?
How and where should i include this line?


Thank you!
0
 
sciuriwareCommented:
That's the commandline you type in a console window or in a shortcut command description.
(properties->target)

so, in stead of          javaw MyProgram         or              javaw -jar MyJar.jar
->                javaw -Xms256m -Xmx512 MyProgram
and              javaw -Xms256m -Xmx512 -jar MyJar.jar

Btw: 256 and 512 are just meeting MY demands, make your own choice.
The Xms number should be lower than the other (you see?)
and don't go too far (2000 is a kind of limit).

;JOOP!
0
 
fionAuthor Commented:
Hi,

The examples given are for standalone application:
>javaw -Xms256m -Xmx512 MyProgram
>and
>javaw -Xms256m -Xmx512 -jar MyJar.jar

How about for those webpages project? how can i assign it into the JVM?

Thx


0
 
objectsCommented:
you need to specify it in the containers startup script.
0
 
fionAuthor Commented:
in server. xml? or another file? probably which file to specify?
0
 
objectsCommented:
no in the shell script that is run to startup the container
0
 
fionAuthor Commented:
I am not sure where is it. It's beyond my knowledge. Can i know where is the shell script located and how is the command would be?  

Thank you!
0
 
fionAuthor Commented:
is it  startup.sh   located in Tomcat\bin?
0
 
objectsCommented:
for tomcat you can use the JAVA_OPTS env variable to specify the vm options
0
 
fionAuthor Commented:
Hi, could you pls give clear instruction how to do it ?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
set JAVA_OPTS=-Xmx512m (Windows)
export JAVA_OPTS=-Xmx512m (Unix)
0
 
sciuriwareCommented:
Don't forget the Xms!   that started this thread!
;JOOP!
0
 
fionAuthor Commented:
do i need to put in JAVA_OPTS in any configuration file?
If not, how does the tomcat knowing   -Xmx512m  assigned for which particular project? Or it's assigning as in whole for any projects within the tomcat?
0
 
girionisCommented:
How does the configuration file starts Tomcat? Paste some code.

>If not, how does the tomcat knowing   -Xmx512m  assigned for which particular project? Or it's
>assigning as in whole for any projects within the tomcat?

It is assigned as a whole for the whole Tomcat instance running. YOu can;t say which object is using how much/what part of memory.
0
 
fionAuthor Commented:
Not sure which file do you?

Tomcat\bin\startup.sh

#!/bin/sh
# -----------------------------------------------------------------------------
# Start Script for the CATALINA Server
#
# $Id: startup.sh,v 1.2 2002/01/15 02:55:38 patrickl Exp $
# -----------------------------------------------------------------------------

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ] ; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done
 
PRGDIR=`dirname "$PRG"`
EXECUTABLE=catalina.sh

# Check that target executable exists
if [ ! -x "$PRGDIR"/"$EXECUTABLE" ]; then
  echo "Cannot find $PRGDIR/$EXECUTABLE"
  echo "This file is needed to run this program"
  exit 1
fi

exec "$PRGDIR"/"$EXECUTABLE" start "$@"



Tomcat\bin\startup.bat

@echo off
if "%OS%" == "Windows_NT" setlocal
rem ---------------------------------------------------------------------------
rem Start script for the CATALINA Server
rem
rem $Id: startup.bat,v 1.4 2002/01/15 02:55:38 patrickl Exp $
rem ---------------------------------------------------------------------------

rem Guess CATALINA_HOME if not defined
if not "%CATALINA_HOME%" == "" goto gotHome
set CATALINA_HOME=.
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
set CATALINA_HOME=..
:gotHome
if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome
echo The CATALINA_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end
:okHome

set EXECUTABLE=%CATALINA_HOME%\bin\catalina.bat

rem Check that target executable exists
if exist "%EXECUTABLE%" goto okExec
echo Cannot find %EXECUTABLE%
echo This file is needed to run this program
goto end
:okExec

rem Get remaining unshifted command line arguments and save them in the
set CMD_LINE_ARGS=
:setArgs
if ""%1""=="""" goto doneSetArgs
set CMD_LINE_ARGS=%CMD_LINE_ARGS% %1
shift
goto setArgs
:doneSetArgs

call "%EXECUTABLE%" start %CMD_LINE_ARGS%

:end

0
 
girionisCommented:
Can you post the catalina.sh?
0
 
fionAuthor Commented:
# OS specific support.  $var _must_ be set to either true or false.
cygwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
esac

# resolve links - $0 may be a softlink
PRG="$0"

while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '.*/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`
CATALINA_HOME=`cd "$PRGDIR/.." ; pwd`
if [ -r "$CATALINA_HOME"/bin/setenv.sh ]; then
  . "$CATALINA_HOME"/bin/setenv.sh
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin; then
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$CATALINA_HOME" ] && CATALINA_HOME=`cygpath --unix "$CATALINA_HOME"`
  [ -n "$CATALINA_BASE" ] && CATALINA_BASE=`cygpath --unix "$CATALINA_BASE"`
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
  [ -n "$JSSE_HOME" ] && JSSE_HOME=`cygpath --path --unix "$JSSE_HOME"`
fi

# Get standard Java environment variables
if [ -r "$CATALINA_HOME"/bin/setclasspath.sh ]; then
  BASEDIR="$CATALINA_HOME"
  . "$CATALINA_HOME"/bin/setclasspath.sh
else
  echo "Cannot find $CATALINA_HOME/bin/setclasspath.sh"
  echo "This file is needed to run this program"
  exit 1
fi

# Add on extra jar files to CLASSPATH
if [ -n "$JSSE_HOME" ]; then
  CLASSPATH="$CLASSPATH":"$JSSE_HOME"/lib/jcert.jar:"$JSSE_HOME"/lib/jnet.jar:"$JSSE_HOME"/lib/jsse.jar
fi
CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/bootstrap.jar

if [ -z "$CATALINA_BASE" ] ; then
  CATALINA_BASE="$CATALINA_HOME"
fi

if [ -z "$CATALINA_TMPDIR" ] ; then
  # Define the java.io.tmpdir to use for Catalina
  CATALINA_TMPDIR="$CATALINA_BASE"/temp
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
  CATALINA_HOME=`cygpath --path --windows "$CATALINA_HOME"`
  CATALINA_BASE=`cygpath --path --windows "$CATALINA_BASE"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  JSSE_HOME=`cygpath --path --windows "$JSSE_HOME"`
fi

# ----- Execute The Requested Command -----------------------------------------

echo "Using CATALINA_BASE:   $CATALINA_BASE"
echo "Using CATALINA_HOME:   $CATALINA_HOME"
echo "Using CATALINA_TMPDIR: $CATALINA_TMPDIR"
echo "Using JAVA_HOME:       $JAVA_HOME"

if [ "$1" = "jpda" ] ; then
  if [ -z "$JPDA_TRANSPORT" ]; then
    JPDA_TRANSPORT="dt_socket"
  fi
  if [ -z "$JPDA_ADDRESS" ]; then
    JPDA_ADDRESS="8000"
  fi
  if [ -z "$JPDA_OPTS" ]; then
    JPDA_OPTS="-Xdebug -Xrunjdwp:transport=$JPDA_TRANSPORT,address=$JPDA_ADDRESS,server=y,suspend=n"
  fi
  CATALINA_OPTS="$CATALINA_OPTS $JPDA_OPTS"
  shift
fi

if [ "$1" = "debug" ] ; then

  shift
  if [ "$1" = "-security" ] ; then
    echo "Using Security Manager"
    shift
    exec "$_RUNJDB" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -sourcepath "$CATALINA_HOME"/../../jakarta-tomcat-4.0/catalina/src/share \
      -Djava.security.manager \
      -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start
  else
    exec "$_RUNJDB" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -sourcepath "$CATALINA_HOME"/../../jakarta-tomcat-4.0/catalina/src/share \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start
  fi

elif [ "$1" = "embedded" ] ; then

  shift
  echo "Embedded Classpath: $CLASSPATH"
  exec "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \
    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
    -Dcatalina.base="$CATALINA_BASE" \
    -Dcatalina.home="$CATALINA_HOME" \
    -Djava.io.tmpdir="$CATALINA_TMPDIR" \
    org.apache.catalina.startup.Embedded "$@"

elif [ "$1" = "run" ]; then

  shift
  if [ "$1" = "-security" ] ; then
    echo "Using Security Manager"
    shift
    exec "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Djava.security.manager \
      -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start
  else
    exec "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start
  fi

elif [ "$1" = "start" ] ; then

  shift
  touch "$CATALINA_BASE"/logs/catalina.out
  if [ "$1" = "-security" ] ; then
    echo "Using Security Manager"
    shift
    "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Djava.security.manager \
      -Djava.security.policy=="$CATALINA_BASE"/conf/catalina.policy \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start \
      >> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
  else
    "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start \
      >> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
  fi

elif [ "$1" = "stop" ] ; then

  shift
  exec "$_RUNJAVA" $JAVA_OPTS $CATALINA_OPTS \
    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
    -Dcatalina.base="$CATALINA_BASE" \
    -Dcatalina.home="$CATALINA_HOME" \
    -Djava.io.tmpdir="$CATALINA_TMPDIR" \
    org.apache.catalina.startup.Bootstrap "$@" stop

else

  echo "Usage: catalina.sh ( commands ... )"
  echo "commands:"
  echo "  debug             Start Catalina in a debugger"
  echo "  debug -security   Debug Catalina with a security manager"
  echo "  embedded          Start Catalina in embedded mode"
  echo "  jpda start        Start Catalina under JPDA debugger"
  echo "  run               Start Catalina in the current window"
  echo "  run -security     Start in the current window with security manager"
  echo "  start             Start Catalina in a separate window"
  echo "  start -security   Start in a separate window with security manager"
  echo "  stop              Stop Catalina"
  exit 1

fi
0
 
objectsCommented:
> do i need to put in JAVA_OPTS in any configuration file?

you need to set it in the environment that starts up tomcat.
0
 
girionisCommented:
I think you should add it in one of these:

 exec "$_RUNJDB" $JAVA_OPTS $CATALINA_OPTS \
      -Xms256m -Xmx512 \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -sourcepath "$CATALINA_HOME"/../../jakarta-tomcat-4.0/catalina/src/share \
      -Dcatalina.base="$CATALINA_BASE" \
      -Dcatalina.home="$CATALINA_HOME" \
      -Djava.io.tmpdir="$CATALINA_TMPDIR" \
      org.apache.catalina.startup.Bootstrap "$@" start

0
 
fionAuthor Commented:
ok, after adding it into catalina.sh  how do i check does it really increased memory?
0
 
girionisCommented:
Ehm... I am not sure. Check some processes in the system and it should tell you how much memory each one is allocated, although I do not know how exactly you can do it.
0
 
fionAuthor Commented:
hm..., i m using windows 2000. I checked the Task Manager on memory. It shows 1 or 2  hundred differents only. I had set JAVA_OPTS  in environment variable.

Here's my code on catalina.sh

  exec "$_RUNJDB" $JAVA_OPTS $CATALINA_OPTS \
    -Xms256m -Xmx512 \
    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
    -sourcepath "$CATALINA_HOME"/../../Tomcat 4.1/catalina/src/share \      
    -Dcatalina.base="$CATALINA_BASE" \
    -Dcatalina.home="$CATALINA_HOME" \
    -Djava.io.tmpdir="$CATALINA_TMPDIR" \
    org.apache.catalina.startup.Bootstrap "$@" stop
0
 
fionAuthor Commented:
It don't really increased the memory if this is the way to check.
0
 
girionisCommented:
The memory should be increased when the default memory size is reached. After that the JVM will start using more memory until the limit is reached again (and it will throw a OutOfMemoryError if you go over the top).
0
 
fionAuthor Commented:
>-sourcepath "$CATALINA_HOME"/../../Tomcat 4.1/catalina/src/share \    

How should i set the sourcepath actually? I tried by removing /../../ It has not any affection... Why it has to be setted /../../?

Thank you!
   
0
 
girionisCommented:
> Why it has to be setted /../../?

I am not sure, I never actualyl had to look through the shell file.
0
 
sciuriwareCommented:
I think that at least some people here answered the question or provided a good insight to get further.
It's only 50 points but the questioner got his/her answer(s).
;JOOP!
0
 
girionisCommented:
The answer is simply "it can't be done" and I see it has already been given.
0
 
fionAuthor Commented:
Hi, It's been very hard to allocate points to you guys. First of all, thanks a lot for the help given.

Points will be given to ojects and girionis on what has been written is true. However, points will be given to sciuriware too as he suggested something new to me with his effort on guiding me.

Thanks a lot to everyone!
0
 
objectsCommented:
Glad we could help :)
0
 
sciuriwareCommented:
If you want to be very popular in EE then throw some more points at people who obviously did more
than just type an answer.

I hope you realise that C means: "may be correct answer but hardly usable".

Maybe you meant A which means: "splendid answer(s), learned a lot this way".

;JOOP!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.