Solved

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

Posted on 2004-08-06
45
254 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:fion
  • 16
  • 11
  • 8
  • +2
45 Comments
 
LVL 92

Accepted Solution

by:
objects earned 20 total points
ID: 11733384
threads aren't allocated memory individually
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 20 total points
ID: 11733401
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
 

Author Comment

by:fion
ID: 11733474
>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
 
LVL 92

Expert Comment

by:objects
ID: 11733486
How many processers are used I think is up to the OS, but could well be wrong.
0
 
LVL 92

Expert Comment

by:objects
ID: 11733493
0
 
LVL 35

Expert Comment

by:girionis
ID: 11733507
> > 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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11733509
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11733515
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
 

Author Comment

by:fion
ID: 11733911
>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
 
LVL 35

Expert Comment

by:girionis
ID: 11734020
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
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 20 total points
ID: 11734297
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
 
LVL 35

Expert Comment

by:girionis
ID: 11734334
The original question is to allocate memory to a "thread" not to the whole JVM :)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11734351
fion may decide about this, and some other people did not know about -Xms ...

;JOOP!
0
 

Author Comment

by:fion
ID: 11749688
>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
 
LVL 24

Expert Comment

by:sciuriware
ID: 11750036
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
 

Author Comment

by:fion
ID: 11759533
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
 
LVL 92

Expert Comment

by:objects
ID: 11759554
you need to specify it in the containers startup script.
0
 

Author Comment

by:fion
ID: 11759649
in server. xml? or another file? probably which file to specify?
0
 
LVL 92

Expert Comment

by:objects
ID: 11759669
no in the shell script that is run to startup the container
0
 

Author Comment

by:fion
ID: 11759693
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
 

Author Comment

by:fion
ID: 11759718
is it  startup.sh   located in Tomcat\bin?
0
 
LVL 92

Expert Comment

by:objects
ID: 11759736
for tomcat you can use the JAVA_OPTS env variable to specify the vm options
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:fion
ID: 11759786
Hi, could you pls give clear instruction how to do it ?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11759799
set JAVA_OPTS=-Xmx512m (Windows)
export JAVA_OPTS=-Xmx512m (Unix)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11759805
Don't forget the Xms!   that started this thread!
;JOOP!
0
 

Author Comment

by:fion
ID: 11760025
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
 
LVL 35

Expert Comment

by:girionis
ID: 11760045
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
 

Author Comment

by:fion
ID: 11760450
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
 
LVL 35

Expert Comment

by:girionis
ID: 11760533
Can you post the catalina.sh?
0
 

Author Comment

by:fion
ID: 11760605
# 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
 
LVL 92

Expert Comment

by:objects
ID: 11760629
> 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
 
LVL 35

Expert Comment

by:girionis
ID: 11760653
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
 

Author Comment

by:fion
ID: 11760847
ok, after adding it into catalina.sh  how do i check does it really increased memory?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11760875
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
 

Author Comment

by:fion
ID: 11760979
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
 

Author Comment

by:fion
ID: 11760984
It don't really increased the memory if this is the way to check.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11760993
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
 

Author Comment

by:fion
ID: 11770354
>-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
 
LVL 35

Expert Comment

by:girionis
ID: 11770370
> Why it has to be setted /../../?

I am not sure, I never actualyl had to look through the shell file.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11998174
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
 
LVL 35

Expert Comment

by:girionis
ID: 12003712
The answer is simply "it can't be done" and I see it has already been given.
0
 

Author Comment

by:fion
ID: 12023955
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
 
LVL 92

Expert Comment

by:objects
ID: 12023971
Glad we could help :)
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12024039
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now