Unable to set JVM parameters/arguments (Xmx and Xms) through .bat file - java

I am able to run a Java program through Eclipse by setting the VM arguments as -Xmx1024m
-Xms256M. Now I want to run the same Java program (jar) through a .bat file in Windows. I am setting the JVM values in the file as follows
#echo off
set JAVA_OPTS="-Xmx1024m -Xms256m -XX:+HeapDumpOnOutOfMemoryError"
java -cp TA.jar com.myClass
But when I run the same program through batch (.bat in Windows XP) it throws Out of Memory error and I suspect that the JVM setting through the .bat file is not working.

It should be _JAVA_OPTIONS instead of JAVA_OPTS.

Use the arguments directly
java -Xmx1024m -Xms256m -XX:+HeapDumpOnOutOfMemoryError -cp TA.jar com.myClass
You don't need to set them at JAVA_OPTIONS. To be sure that your application is using the parameters that you want:
open jvisualvm that comes with java. Just type "jvisualvm" at command line if you have set java correctly at your path.
open the vm started to your application.
check under "JVM Arguments" in the "Overview" tab.
There should be set your jvm options.

Related

how to change the amount of java memory heap for Jason-JEdit generated processes? [Linux]

how to change the amount of java memory heap for Jason-JEdit generated processes?
I'm running code edited in Jason-JEdit, as part of the application a NetLogo process is started, originated by the first one, but a heap size error appears.
The main process in Jason-Jedit is:
user 6006 6003 9 19:44 ? 00:00:02 java -Xms256m -Xmx1024m -classpath ~/Jacamo/lib/jacamo.jar:/usr/local/lib/netlogo5.2/NetLogo.jar:. -jar ~/Jason-1.4.2/bin/jedit/jedit.jar
The generated process from Jason-Jedit are:
1-The normal launcher process of the agent
user 6055 6006 11 19:44 ? 00:00:00 /usr/lib/jvm/java-7-oracle/bin/java -classpath ~/Jacamo/lib/ant-launcher.jar org.apache.tools.ant.launch.Launcher -e -f bin/build.xml run
2- The NetLogo lauched process
user 6069 6055 99 19:44 ? 00:00:05 /usr/lib/jvm/java-7-oracle/jre/bin/java -classpath ~/JaCaMo/workingDir:~/JaCaMo/workingDir/bin/classes:~/Jacamo/lib/jason.jar:/usr/local/lib/netlogo5.2/NetLogo.jar jason.infra.centralised.RunCentralisedMAS systemMAS-JNL.mas2j
As it can be seen the generated processes do not have the heap size option: -Xms256m -Xmx1024m, as the first one process does.
I already tried the following:
1) Edit the jason.sh/Jacamo.sh files adding:
java -Xms256m -Xmx1024m -classpath [...]
2) Add the variable _JAVA_OPTIONS in the .profile
export _JAVA_OPTIONS="-Xms256m -Xmx1024m"
And this works for the main process only, the Jason-Jedit application, but the problem persist in the generated process. Any idea about how to change the settings in JEdit?
I don't think this has anything to do with jEdit itself, as it is just shipped and used as editor and host for the custom plugin the jason project is providing. From a very short look at the plugin code, I'd say it generates an Ant build script that starts the RunCentralisedMAS class and it doesn't inject any jvmargs into this Ant build script. But the build-template.xml file has a commented-out line with jvmargs. So I guess if you search for the build-template.xml file and change it so that the jvmarg are set, it will work for you.

"Invalid maximum heap size" when running Maven

I'm having problems setting up maven. I'm using windows 7 64 bit with 4 GB of RAM.
I get this error when trying to run mvn:
Invalid maximum heap size: -Xmx512m.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Here are my user variables:
JAVA_HOME=C:\Program Files\Java\jdk1.7.0_55
M2=%M2_HOME%\bin
M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.2.1
MAVEN_OPTS=-Xms256m -Xmx512m
Path=%M2%;%JAVA_HOME%\bin
User #med_alpa had the right suggestion in my case (Windows):
I resolved the same issue on windows executing this command on windows
command line : set MAVEN_OPTS=-Xmx1024M -XX:MaxPermSize=256M
So make sure to not put quotes around the value you set for MAVEN_OPTS
alter jvm.config in .mvn directory ( it is in controller directory ).
study mvn.sh that comes with maven. you will see MAVEN_OPTS setting in the script.
try out java -version before altering.
eg : java -Xmx512m -version
now you know how to go about and make the changes.
/sunil
Try using: -Xmx1024M
Note: 'M' instead 'm'
On MacOS a version of #adriaan-koster's answer works.
In your ~/.zshrc (or .bashrc or .bash_profile) config file add this line:
export MAVEN_OPTS="-Xmx2G"
To test, run mvn -v again and verify that it prints the Maven version, Maven home location, and Java version.

Is there a .jar launcher for OS X

I am under the impression that Launch4J cannot create launchers for OS X. Correct me, if I am wrong.
I would like to use something similar so I can set the initial heap size/ max heap size for my Java application without using command line options (java -jar -Xmx1024m etc....).
Any recommendations?
What you want is an Application Bundle. It include a properties file (Info.plist) where you can set things like Xmx, etc.
You can also use Oracle's appbundler tool to create an application bundle.
Another no-brainer option is to create a shell script. Something like this:
#!/bin/bash
java -jar myJar.jar
Eclipse can export a run configuration as a runnable jar file or a Mac OSX application bundle.
In the run configuration, add the command line options to the VM arguments textbox in the arguments tab.

Running java with JAVA_OPTS env variable has no effect

In a shell script, I have set the JAVA_OPTS environment variable (to enable remote debugging and increase memory), and then I execute the jar file as follows:
export JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n -Xms512m -Xmx512m"
java -jar analyse.jar $*
But it seems there is no effect of the JAVA_OPTS env variable as I cannot connect to remote-debugging and I see no change in memory for the JVM.
What could be the problem?
PS: I cannot use those settings in the java -jar analyse.jar $* command because I process command line arguments in the application.
You can setup _JAVA_OPTIONS instead of JAVA_OPTS. This should work without $_JAVA_OPTIONS.
I don't know of any JVM that actually checks the JAVA_OPTS environment variable. Usually this is used in scripts which launch the JVM and they usually just add it to the java command-line.
The key thing to understand here is that arguments to java that come before the -jar analyse.jar bit will only affect the JVM and won't be passed along to your program. So, modifying the java line in your script to:
java $JAVA_OPTS -jar analyse.jar $*
Should "just work".
In the past 12 years some changes were made.
Environment variable JAVA_OPTS was and is NOT a standardized option. It is evaluated by some shell script wrappers for Java based tools, an example of how this works is in the answer from ZoogieZork.
The environment variable _JAVA_OPTIONS mentioned by HEX is nowadays deprecated/undocumented.
Starting with Java 9, the recommended way to do what you wanted is the variable JDK_JAVA_OPTIONS, see Using the JDK_JAVA_OPTIONS Launcher Environment Variable in the Oracle Java 9 documentation, and this comprehensive answer What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?.

increase the java heap size permanently?

Is there a way that I can set the default heap size for the jvm on my own computer? I want to set it to 1g, because I'm always running custom programs that always hit the overage point in the default jvm size.
I just dont want to have to remember to type -XmX1g everytime I run my java app from the command line...
There has to be an admin way to do this right?
Apparently, _JAVA_OPTIONS works on Linux, too:
$ export _JAVA_OPTIONS="-Xmx1g"
$ java -jar jconsole.jar &
Picked up _JAVA_OPTIONS: -Xmx1g
For Windows users, you can add a system environment variable named _JAVA_OPTIONS, and set the heap size values there. The JVM should be able to grab the virtual machine options from _JAVA_OPTIONS.
This worked for me:
export _JAVA_OPTIONS="-Xmx1g"
It's important that you have no spaces because for me it did not work. I would suggest just copying and pasting. Then I ran:
java -XshowSettings:vm
and it will tell you:
Picked up _JAVA_OPTIONS: -Xmx1g
what platform are you running?..
if its unix, maybe adding
alias java='java -Xmx1g'
to .bashrc (or similar) work
edit: Changing XmX to Xmx
if the platform is Linux, then adding an entry in bash_profile will help.
vim ~/.bash_profile
then add
export _JAVA_OPTIONS="-Xmx4g"

Categories