Set heap size for -jar command - java

I want to set heap size under Linux OS in order to use it on java -jar command.
I have tried to add _JAVA_OPTIONS property under .bash_profile, but this property is ignored. I know I can run my jar with
java $_JAVA_OPTIONS -jar
command or create an alias like
alias java='java -Xms256m -Xmx512m'
I have found I can also export _JAVA_OPTIONS:
export _JAVA_OPTIONS="-Xms256m -Xmx512m"
But this property is not kept between sessions.
Any idea how can I set permanent heap size for java -jar command?

Related

How to put '-XX:+UseG1GC -Xmx1g -Xms512m' parameters for a .jar in the service wrapper?

I have a jar named my-app-0.0.1-SNAPSHOT.jar. I have to pass -XX:+UseG1GC -Xmx1g -Xms512m to the jar like
sudo java -XX:+UseG1GC -Xmx1g -Xms512m -jar my-app-0.0.1-SNAPSHOT.jar
I've created the .sh using above command. I use this single line bash script file in my-app.service to execute the .jar. Now I need to put everything in .service file. No .sh in picture. How to put those in the .service file? Any help is appreciated.

How to set heap size for wildfly inside Docker container?

I am trying to increase heap size for wildfly in docker container. This is easily done by updating wildfly/bin/standalone.conf in a regular wildfly setup.
Our base docker image for wildfly has a default heapsize of 512 MB which is required to be 1GB in one of the web apps. One way to go is by simple text replace in the Docker file using sed command -
RUN sed -i -- 's/JAVA_OPTS="-Xms64m -Xmx512m -XX:MaxPermSize=256m/JAVA_OPTS="-Xms2048m -Xmx6144m -XX:MaxPermSize=256m/g' /path/standalone.conf
I wanted to know if there's another (cleaner) way to solve this?
When using docker-compose set environment variable as follows!
environment:
- JAVA_OPTS=-server -Xms512m -Xmx2048m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -XX:+UseAdaptiveSizePolicy -XX:MaxMetaspaceSize=1024m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true-Djava.net.preferIPv4Stack=true
do not use " (quotes)!
You can pass the value of the JAVA_OPTS environment variable in the command used to run the docker container:
docker run -it --env JAVA_OPTS="-server -Xms2048m -Xmx6144m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true" jboss/wildfly
Alternatively, you can extend the standard image by creating a Dockerfile containing:
FROM jboss/wildfly:latest
COPY standalone.conf $JBOSS_HOME/bin/
and placing your modified standalone.conf in the directory next to it.
Then you can build it:
docker build -t my/wildfly:latest .
and run it:
docker run my/wildfly
I suggest you to use JAVA_TOOL_OPTIONS rather than JAVA_OPTS .Because JVM directly understand JAVA_TOOL_OPTIONS.
I haven't tried it myself, but there is now (at least WF26) a variable JBOSS_JAVA_SIZING in standalone.conf, used as a subpart of JAVA_OPTS.
if [ "x$JBOSS_JAVA_SIZING" = "x" ]; then
JBOSS_JAVA_SIZING="-Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m"
fi
if [ "x$JAVA_OPTS" = "x" ]; then
JAVA_OPTS="$JBOSS_JAVA_SIZING -Djava.net.preferIPv4Stack=true"
...
So you should be able to rewrite it through docker environment variables.

Set Java path in command line for only one directory

I am running a program that utilizes Scala 2.10 for work and is not compatible with Java 8, only Java 7. In a Windows 7 command line, how can I set the java path to use Java 7 ONLY for that directory?
You may create 2 batch file one for java 7 and one for java 8 like this -
jdk7.bat
#echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.0_11\bin;%PATH%
echo Display java version
java -version
jdk8.bat
#echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_11
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.7.8_11\bin;%PATH%
echo Display java version
java -version
You may quickly switch between them running these batch file.
If the program uses a batch to start, then add this line before the start of the program:
SET JAVA_HOME="C:\Program Files\Java7\Java.exe"
(This is just an example, the directory might be different on your computer)
If the program does not use such a batch (you can recognize it because it ends with either .cmd or .bat) create such a file and use that for launching the program instead:
#echo off
SET JAVA_HOME=...
ThisIsMyFancyScalaProgram.Exe
To Add system environment variables:
setx JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx PATH "%PATH%;%JAVA_HOME%\bin";
To update system environment variables:
setx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0"
setx -m PATH "%PATH%;%JAVA_HOME%\bin";

TOMCAT 7, Can't change the heap size

I Have set the heap size of tomcat 7 by adding boot script:
export CATALINA_OPTS="-Xms1024m -Xmx248m"
I change /etc/init.d/tomcat7 :
if [ -z "$JAVA_OPTS" ]; then
JAVA_OPTS="-Djava.awt.headless=true -Xmx2048M -Xms1024M"
fi
I Reboot the computer and restart the Tomcat:
service tomcat7 restart
And verify the $CATALINA_OPTS Works:
> echo $CATALINA_OPTS
-Xms1024m -Xmx2048m
But when I go to the tomcat manager, I note that the heap has not changed.
Free memory: 38.02 MB Total memory: 123.75 MB Max memory: 123.75 MB
Please, i need help about this.
Check the setenv.sh in tomcat/bin, according to manual this should be the right place to put those params.
Another option, it depend on your OS tomcat package, may be that config param are overrided in /etc/conf.d/tomcat/ or /etc/tomcat. Just check your init script and your catalina.sh to find where your settings are overrided.
Btw if you run a ps -ef | grep tomcat you should see the full command line with arguments: this may give you an idea of how init script build the command, and so you can investigate where params are set.
Have you tried creating a setenv.sh script in the $CATALINA_HOME/bin directory with your options in it?
I find that setting JAVA_OPTS="-Xmx2048m -Xms1024m" in there works pretty well.

Error executing: java -Xms512M -Xmx1024M

$ java -Xms512M -Xmx1024M
While running the above command I got the error below. Please help me how to set JVM Heap memory in AIX box. Here I'm using java 5.
Usage: java [-options] class [args...]
(to execute a class) or java -jar [-options] jarfile [args...]
(to execute a jar file)
where options include:
-cp -classpath <directories and zip/jar files separated by :>
set search path for application classes and resources
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version
-showversion print product version and continue
-? -help print this help message
-X print help on non-standard options
Well, you have to execute SOME java program, not just the JVM. The options itself are fine. Try java -Xms512M -Xmx1024M -cp somejar.jar mystuff.Main or something.
What about export JAVA_OPTS="-Xms512M -Xmx1024M" (unix) or set JAVA_OPTS=-Xms512M -Xmx1024M (windows)?
Try This also :
java -Xms512M -Xmx1024M -classpath jar1.jar:jar2.jar packageName.YourMainClassName Arguments

Categories

Resources