Disabling Java huge-pages system-wide on a RH machine - java

I know I can disable Java's use of huge-pages for a process by adding
-XX:-UseLargePages
to the process invocation line.
However, I'd like to prevent every java application from using huge-pages, without me having to discover each process running on the machine.
Disabling THP in RH does not do it. Java still, by default, allocate memory from huge-pages even when THP is disabled.

Try to set this in the environment variable JAVA_TOOL_OPTIONS
See http://www.oracle.com/technetwork/java/javase/envvars-138887.html for details
I checked this with my Eclipse installation. Before starting it I set the variable with set JAVA_TOOL_OPTIONS="-Dfoo=123 -Dbar=456" (using export on the command line or setting it in the environment file will do the same trick on Linux).
Checking the JVM with visualvm shows that the new parameters are considered:
On the console or the corresponding logfile you will most likely see an entry like this:
Picked up JAVA_TOOL_OPTIONS: "-Dfoo=123 -Dbar=456"

Related

The use of Java options environment variables detected. How to delete it?

When I turned Android Studio on Windows 10, I got this warning:
The use of Java options environment variables detected. Such variables
override IDE configuration files (*.vmoptions) and may cause
performance and stability issues. Please consider deleting these
variables: JAVA_TOOL_OPTIONS.
So, I want to delete this variable, but I don't know how. This similar query didn't help me because I don't see this variable in system variables. I suspect I must have set it via the command line when I was troubleshooting another issue. When starting the console of any IDE, the message:
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
Is also displayed. Can it be deleted somehow?
As mentioned in the other questions, you get that warning if starting any java process when the JAVA_TOOL_OPTIONS environment variable is declared. This is mentioned in Windows Android Studio logs on start-up, and also if you run java -version from a terminal / cmd.exe.
Before removing first check whether you need these addition settings or not. The setting -Dfile.encoding=UTF8 is default for JDK19 onwards, but not for earlier JDKs and fixing to avoid an Android Studio warning might cause unexpected changes elsewhere with other Java based applications on pre-JDK19.
Two places I know where it could be defined:
Settings > System > About > Advanced System Settings -> Environment Variables:
Delete JAVA_TOOL_OPTIONS values under your user settings or System settings.
This setting can be added back by re-entering in the dialog or typing command: setx JAVA_TOOL_OPTIONS blah.
Don't forget to close/re-open Windows Terminal or CMD.EXE after making changes to the environment variables or they won't see the new values.
As a local variable in CMD.EXE. This setting may have been added by some init script (say if you use CMD.EXE /k init.cmd) calling set JAVA_TOOL_OPTIONS=blah and this overrides / replaces 1) value.
You may be able to remove with set JAVA_TOOL_OPTIONS= and re-running java or Android Studio from that CMD, but note that the original value will be restored after close/re-open CMD if it was set in 1) already.
There is no doubt an easier way to do this as annoyingly it adds a blank cmd terminal: if you wished to disable for just Android Studio you could set up an alternative windows shortcut with this as "Target" field (do no add any extra spaces):
cmd.exe /c set JAVA_TOOL_OPTIONS=&&"C:\Program Files\Android\Android Studio\bin\studio64.exe"
Note:
setx VARNAME NEWVALUE does not need "=" sign.
set VARNAME=NEWVALUE requires the "=" sign

Adding JVM system properties to running JVM?

I have a running Java program which I would like to control at the command line by changing a custom system property which I'm listening for changes to inside the application, thereby not requiring a restart of the application for the changes to take affect.
I understand that it's possible to change some of the default JVM properties using jinfo but it doesn't seem to work for custom properties which I set at compile time using the -D flag.
For example, if I start a JAR using the following command line arguments:
java -Dfoo=1 -jar my_jar.jar
attempting to call the following fails:
jinfo -flag foo=2 'pid'
Is there something I'm missing or is this simply not possible?
This utility is unsupported and might not be available in future releases of the JDK. Official Doc
Not all of the available parameters can be changed. Reference
IMO, Recommended way is to have some event-listener or service call to change the flags of running application.
I had used some system properties using jinfo so not sure if custom flags can be changed during jar execution. Try some custom boolean flag using command jinfo -flag +<customBooleanFlag> <PID> to check if its working.

System.getenv() does not list all the environment variables

I have noticed that some of my environment variables are not being picked up by the JVM.
In my .bash_profile I defined the following:
IO_HOME='some_value'
export IO_HOME
and by doing in shell:
echo $IO_HOME
I get the correct result.
But neither System.getProperties() nor System.getenv() is showing this variable being set. I tried both Java 6 and Java 7.
Is there something I am missing?
Exporting environment to spawned processes is pretty stable; if System.getenv() is not including a variable then it is because it is not in the environment. A couple of things to check, both relating to how the process is started:
Are you starting the java process from an environment where the variable is exported? For example, if it is in your .bash_profile and you are executing the java program from a menu or desktop then you have to log out and log in after adding it in .bash_profile for your desktop to see the variable.
Is the variable explicitly removed from environment for the process? ProcessBuilder allows this, as do most of all APIs that spawn processes.
One thing to try is to start the process from command line shell, after ensuring the variable is exported in that shell.
From Windows, I recently saw some crazy behaviour where IntelliJ refused to show all env vars from System.getenv() after setting either user or system env vars. The trick was to launch IntelliJ from a DOS box. For Windows users: Maybe a reboot (or logoff/logon) will fix the issue.

How to set a java system property so that it is effective whenever I start JVM without adding it to the command line arguments

There was a change in Java 1.7 in the way the default Locale is get from the OS. There are methods of restoring the old behaviour e.g. by setting the flag -Dsun.locale.formatasdefault=true when starting a JVM instance.
I would like to set this flag permanently so that I don't have to specify it in command line arguments each time when I start a JVM instance. Is there a file or any other possibility to change the default settings for JVM? Something like the Eclipse.ini file but for the JVM itself?
You can set set environment variable JAVA_TOOL_OPTIONS in your OS. All Java tools (java, javac, ..) will pick this variable up and use it. So you could e.g. use
SET JAVA_TOOL_OPTIONS=-Dsun.locale.formatasdefault=true
I use this to force a specific locale for each JVM.
But this only works if your application is started through the Java tools. If it is e.g. started from a C program that calls the jvm DLL this won't be used.
Edit: I just tested it, and it seems JAVA_TOOLS_OPTIONS is also picked up when the DLLs are started (verified with a Swing application that uses WinRun4J as a launcher)
See: http://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/envvars.html

Setting environment variable permanently from java

Using Java,
How can I add environment variable permanently to the existing env variables.
so that when I do a restart operation for windows or Linux, this environment variable is still there.
You might want to take a look at this.
In Windows you can set a Path Variable from command line so it should do the trick.
I realize this is only applicable to Windows.
Not in any cross platform sort of way. In Linux, these are typically controlled via shell init scripts. You would have to edit one of those (which one depends on the user, system, and shell type). In Windows, this is controlled via system configuration (i'd imagine there are some windows specific APIs to modify those).
coppy the path of jdk upto C:\Program Files\Java\jdk1.6.0\bin from program and past in user variables and put ;.; at the end and give name .
and in system variables click on new and enter the name and past the path....and save ...
go to command prompt
..
to check current paths >echo %path%
to set path >set path="C:\Program Files\Java\jdk1.6.0\bin" enter ok now check and run java program
Environment variables are platform specific. Windows stores them in Registry.
*In the registry the User environment variables are stored at
HKEY_CURRENT_USER\Environment
and the System environment variables are stored at
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
(from http://demins.blogspot.co.il/2007/10/where-does-windows-xp-store-evrironment.html)*
There are a lot of ways to access windows registry from java. You can for example execute command line using utility named reg that has a reach command line. You can also use one of interoparability APIs like JaWin, Jinterop, Jintegra. You can also refer to my solution explained here.
On linux you can use command line like export MYVAR=myvalue. I mean execute this command line from java using Runtime.exec() or ProcessBuilder. The problem is that this variable will not become really persistent. It will be visiable for all users until the computers is restarted. To make it really persistent you have to modify user login script (e.g. bashrc file for most linux systems if users's default shell is bash).

Categories

Resources