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:
Related
I have a java program whose only function is to use about 512M RAM and be idle. Running natively it works:
java -Xmx559m MemoryEater
When I run the same program with the same memory settings in Docker, Java errors out with an out of memory error. That's java not docker killing it. I have to nearly double heap size to have a successful run.
docker run -it --rm -e JAVA_OPTIONS='-Xmx1027m' memory-eater memory-eater
Why is this? I did try setting different levels of docker --memory and --memory-swap but the 1027m heap size is always required.
Additional Info:
Error message:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at MemoryEater.main(MemoryEater.java:10)
Program:
import java.util.Vector;
public class MemoryEater
{
public static void main(String[] args)
{
Vector v = new Vector();
for( int i=1; i<512; i++)
{
byte b[] = new byte[1048576];
v.add(b);
Runtime rt = Runtime.getRuntime();
System.out.println( "i = " + i + " free memory: " + rt.freeMemory() + " max memory: " + rt.maxMemory() + " total memory: " + rt.totalMemory() );
}
while( true ){}
}
}
Both native and container are 64bit.
In your docker command I do not see that you are restricting memory usage in any way. That means docker will allow the container to use whatever the host is able to give. With that your JVM will (hopefully) die for memory before docker gets active.
Check out the available options:
https://docs.docker.com/config/containers/resource_constraints/
And use docker statsto display actual resource usage and limits.
https://docs.docker.com/engine/reference/commandline/stats/
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 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.
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.
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