Get the pagefile size of a drive - java

How can I find the current pagefile size of a drive?
Priror to windows 7, There used to be a script named pafefileconfig.vba in System32 folder which can be used. But its now removed.
Is it possible to get the details using JNA? If yes, how?
EDIT
This is the code i wrote to get the pagefile info using JNA:
Kernel32 kernel32 = (Kernel32)Native.loadLibrary("kernel32", Kernel32.class);
MEMORYSTATUSEX memInfo = new MEMORYSTATUSEX();
kernel32.GlobalMemoryStatusEx(memInfo);
int toMB = (1024*1024);
float RAM = memInfo.ullTotalPhys.floatValue();
float totalPage = memInfo.ullTotalPageFile.floatValue();
float availPage = memInfo.ullAvailPageFile.floatValue();
float availRam = memInfo.ullAvailPhys.floatValue();
System.out.println(memInfo.dwMemoryLoad);
System.out.println("RAM "+RAM/toMB);
System.out.println("RAM avail "+availRam/toMB);
float ramUsed = RAM-availRam;
System.out.println("RAM used "+ramUsed/toMB);
System.out.println("Total page(RAM+Page) "+(totalPage)/toMB);
float totalPageWithoutRam = totalPage-RAM;
System.out.println("Total page(without RAM) "+(totalPageWithoutRam)/toMB);
System.out.println("Total avail page(With free ram) "+availPage/toMB);
float avialPageWithoutRam = availPage-availRam;
System.out.println("Total page avail(Without ram) "+(avialPageWithoutRam)/toMB);
System.out.println("Page used so far(Without ram) "+(totalPageWithoutRam-avialPageWithoutRam)/toMB);
And this is the output:
82
RAM 12285.582
RAM avail 2167.6758
RAM used 10117.906
Total page(RAM+Page) 24569.348
Total page(without RAM) 12283.766
Total avail page(With free ram) 12115.641
Total page avail(Without ram) 9947.965
Page used so far(Without ram) 2335.8008
I got the same result on using GetPerformanceInfo aswell.
But this looks different from what i get when i run wmic pagefile
wmic:root\cli>pagefile list /format :list
AllocatedBaseSize=12285
CurrentUsage=843
Description=C:\pagefile.sys
InstallDate=20120329043502.876449+330
Name=C:\pagefile.sys
PeakUsage=843
Status=
TempPageFile=FALSE
Why I am seeing a difference?

Well the information is exposed through WMI, you can use the wmic command line tool to list the pagefile information.
e.g. on my desktop:
C:\WINDOWS\system32>wmic pagefile list /format:list
AllocatedBaseSize=3840
CurrentUsage=213
Description=C:\pagefile.sys
InstallDate=20110616154020.168800+060
Name=C:\pagefile.sys
PeakUsage=231
Status=
TempPageFile=FALSE
You could integrate it using ProcessBuilder, etc...
As it was asked, this is exposing the Win32_PageFileUsage structure, which defines the sizes in MB.

Related

Criteria for default garbage collector Hotspot JVM 11/17

I found a source describing that the default gc used changes depending on the available resources. It seems that the jvm uses either g1gc or serial gc dependnig on hardware and os.
The serial collector is selected by default on certain hardware and operating system configurations
Can someone point out a more detailed source on what the specific criteria is and how that would apply in a dockerized/kubernetes enivronment.
In other words:
Could setting resource requests of the pod in k8s to eg. 1500 mCpu make the jvm use serial gc and changing to 2 Cpu change the default gc to g1gc? Do the limits on when which gc is used change depending on jvm version (11 vs 17)?
In JDK 11 and 17 Serial collector is used when there is only one CPU available. Otherwise G1 is selected
If you limit the number of CPUS available to your container, JVM selects Serial instead of the defaultG1
JDK11
1 CPU
docker run --cpus=1 --rm -it eclipse-temurin:11 java -Xlog:gc* -version
[0.004s][info][gc] Using **Serial**
It uses Serial
More than one CPU
docker run --cpus=2 --rm -it eclipse-temurin:11 java -Xlog:gc* -version
[0.008s][info][gc ] Using G1
It uses G1
JDK17
1 CPU
docker run --cpus=1 --rm -it eclipse-temurin:17 java -Xlog:gc* -version
[0.004s][info][gc] Using Serial
It uses Serial
More than one CPU
docker run --cpus=2 --rm -it eclipse-temurin:17 java -Xlog:gc* -version
[0.007s][info][gc] Using G1
It uses G1
In current OpenJDK, G1 GC is chosen by default for the "server class machine", or Serial GC otherwise. "Server class machine" is defined as a system with 2 or more non-HT CPUs and 2 or more GiB RAM.
The exact algorithm can be found in src/hotspot/share/runtime/os.cpp:
// This is the working definition of a server class machine:
// >= 2 physical CPU's and >=2GB of memory, with some fuzz
// because the graphics memory (?) sometimes masks physical memory.
// If you want to change the definition of a server class machine
// on some OS or platform, e.g., >=4GB on Windows platforms,
// then you'll have to parameterize this method based on that state,
// as was done for logical processors here, or replicate and
// specialize this method for each platform. (Or fix os to have
// some inheritance structure and use subclassing. Sigh.)
// If you want some platform to always or never behave as a server
// class machine, change the setting of AlwaysActAsServerClassMachine
// and NeverActAsServerClassMachine in globals*.hpp.
bool os::is_server_class_machine() {
// First check for the early returns
if (NeverActAsServerClassMachine) {
return false;
}
if (AlwaysActAsServerClassMachine) {
return true;
}
// Then actually look at the machine
bool result = false;
const unsigned int server_processors = 2;
const julong server_memory = 2UL * G;
// We seem not to get our full complement of memory.
// We allow some part (1/8?) of the memory to be "missing",
// based on the sizes of DIMMs, and maybe graphics cards.
const julong missing_memory = 256UL * M;
/* Is this a server class machine? */
if ((os::active_processor_count() >= (int)server_processors) &&
(os::physical_memory() >= (server_memory - missing_memory))) {
const unsigned int logical_processors =
VM_Version::logical_processors_per_package();
if (logical_processors > 1) {
const unsigned int physical_packages =
os::active_processor_count() / logical_processors;
if (physical_packages >= server_processors) {
result = true;
}
} else {
result = true;
}
}
return result;
}

java.lang.OutOfMemoryError: Java heap space in rserve java

I am running an Rscript from Java which is having two functions one will return a data frame and other will return the column name of the data frame returned by function 1.
The code is running fine for 400 MB data but when I am testing with more than 400 MB it is throwing exception: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
I have tried many solutions for this to increase the heap size.
The solutions I have tried are:-
I have edited the domain.xml file of glassfish.
I have tried to set fore fully Java option using "set _JAVA_OPTIONS = -Xms1025m -Xmx4gm" and export JAVA_OPT= -Xms1025m -Xmx4g.
Edited the netbeans.conf file as
netbeans_default_options="-J-Xms1025m -J-Xmx4g -J-Xss32m -J-XX:PermSize=128m -J-XX:MaxPermSize=2g -J-Xverify:none -J-Dapple.laf.useScreenMenuBar=true -J-Dapple.awt.graphics.UseQuartz=true -J-Dsun.java2d.noddraw=true -J-Dsun.java2d.dpiaware=true -J-Dsun.zip.disableMemoryMapping=true"
Crontab with Java option -Xms1025m -Xmx4gm
Changed the netbeansproject->properties->run->vmoption also.
But still getting the same error.
And when I tried to check the heap size using the below Java code. It is showing something different. I am not getting which is correct and which is wrong. The heap size is changed or not. How to know it and how to solve this?
I got this code from some website while searching for the error.
The output I am getting is
run:
JVM freeMemory: 10
JVM totalMemory also equals to initial heap size of JVM : 10
JVM maxMemory also equals to maximum heap size of JVM: 168
Used Memory in JVM: 162
freeMemory in JVM: 6
totalMemory in JVM shows current size of Java heap : 15
maxMemory in JVM: 168
JVM Bit size: amd64
Heap Size = 163123200
public class MemoryUtil
{
private static final int MegaBytes = 10241024;
public static void main(String args[]) {
long freeMemory = Runtime.getRuntime().freeMemory()/MegaBytes;
long totalMemory = Runtime.getRuntime().totalMemory()/MegaBytes;
long maxMemory = Runtime.getRuntime().maxMemory()/MegaBytes;
System.out.println("JVM freeMemory: " + freeMemory);
System.out.println("JVM totalMemory also equals to initial heap size of JVM : "
+ totalMemory);
System.out.println("JVM maxMemory also equals to maximum heap size of JVM: "
+ maxMemory);
ArrayList objects = new ArrayList();
for (int i = 0; i < 10000000; i++) {
objects.add(("" + 10 * 2710));
}
freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;
totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;
System.out.println("Used Memory in JVM: " + (maxMemory - freeMemory));
System.out.println("freeMemory in JVM: " + freeMemory);
System.out.println("totalMemory in JVM shows current size of java heap : "
+ totalMemory);
System.out.println("maxMemory in JVM: " + maxMemory);
System.out.println("JVM Bit size: " + System.getProperty("os.arch"));
long heapSize = Runtime.getRuntime().totalMemory();
//Print the jvm heap size.
System.out.println("Heap Size = " + heapSize);
}
}
To change the maximum heap size for the applications running inside Glassfish you only have to edit the setting in your domain.xml.
Stop Glassfish, open the file (/GLASSFISH_HOME/glassfish/domains/domain1/config/domain.xml for those who don't know) with a text editor and search for Xmx (the first result), it should look like this:
<jvm-options>blabla</jvm-options>
<jvm-options>-Xmx512m</jvm-options>
It may contain another value if you already touched this. Change the value to something like this:
<jvm-options>blabla</jvm-options>
<jvm-options>-Xmx4096m</jvm-options>
Note that Glassfish itself requires some memory to run, I suggest you add additional 350 MB only for Glassfish on your setting, if your hardware specs allow that.
Alternativly you can use the Glassfish Admin UI to change the setting:

how do i increase Heap Memory Size Programatically

i have sample code for increase Heap Memory.But it is not increasing Memory.
sample Code :
int mb=1024*1024;
long rt=Runtime.getRuntime().totalMemory();
int heapsize=(int) (rt/mb);
System.out.println("Heap Size : " +heapsize);
String[] cmd = {"cmd.exe", "/c", "cd/C C:\\Users\\xxxxxx\\Documents\\NetBeansProjects\\MultiThreadSample\\src\\multithreadsample && java -Xms61m -Xmx128m"};
Process exec = Runtime.getRuntime().exec(cmd);
exec.destroy();
SpawnAndChangeHeap is the class name.Can You Please Suggest Me?
java heap allocation are continuous and happens at JVM initialization. Programmatically heap size cannot be extended or modified until Xmx or Xms changed with JVM restart.

How there is no affect of setting memory through System.setProperty?

import java.io.*;
import java.util.*;
public class ReadPropertiesFile {
public static void main(String[] args)throws Throwable{
Properties prop = new Properties();
prop.load(new FileInputStream(
"C:\\Windows\\Sun\\Java\\Deployment\\deployment.properties"));
String Xmx = prop.getProperty("deployment.javaws.jre.0.args",
"This is Default");
if(Xmx!="This is Default")
{
System.setProperty("javaplugin.vm.options","\"Xmx\"");
}
long maxMemory = Runtime.getRuntime().maxMemory();
System.out.println("JVM maxMemory also equals to maximum heap size of JVM: "
+ maxMemory);
}
}
It should print the value of maxMemory around 96MB(for 2 gb RAM) when nothing specified in the deployment.properties AND 512MB when explicitly mentioning the deployment.javaws.jre.0.args=-Xmx512m.But in both case i am getting the result 259522560
The JVM memory parameters for a Hotspot Java implementation can only be set via the command line options when the JVM is launched / started. Setting them in the system properties either before or after the JVM is launched will have no effect.
What you are trying to do simply won't work.
By the time any Java code is able to run, it is too late to change the heap size settings. It is the same whether you run your code using the java command, using web start, using an applet runner, using an embedded JVM in a browser ... or any other means.

can a java EE app use more than MAX heap memory

I checked with following code in my servlet:
int mb = 1024 * 1024;
Runtime runtime = Runtime.getRuntime();
out.write("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);
out.write("Free Memory:" + runtime.freeMemory() / mb);
out.write("Total Memory:" + runtime.totalMemory() / mb);
out.write("Max Memory:" + runtime.maxMemory() / mb);
the output is :
Used Memory:10
Free Memory:46
Total Memory:57
Max Memory:57
I want my app not to use more than 64 MB heap? I want to know - is there any way my app can use more then 64 MB heap...(Max Memory:57) ? ..will my app throw an OutOfMemoryException after 57MB?
Default Max Heap size is 64Mo, the only way to get more is to set the max size with this parameter :
-Xmx256m
While this parameter set the start value :
-Xms128m
Don't forget the "m" at the end which means Megabytes, don't give more start memory than max neither.
Furthermore, there is no setMaxHeapSize() function if it's what you are looking for.
If you are runnning your servlet inside a tomcat you should try with this :
export CATALINA_OPTS=-Xms16m -Xmx256m;
in startup.bat
If your servlet need more than JVM can allow, you will get weird errors when reaching the limit and most of the time a OutOfMemoryException, the Garbage Collector will try to avoid that though so it will crawl a bit before throwing it.
When you run your application you must also specify the max heap size.
For example if your class name is HelloWorld and if you want to set 512mb the heap size then you have to launch te following command:
java -Xmx512m HelloWorld

Categories