I am working on a project in which execution at some size of data requires additional heap space, The actual problem is that I tried multiple approaches to solve but all in vain.
At first, I started by editing my launch.json file since I am using VSCode, tried adding the Xmx parameter to the vmArgs. nothing, I added a portion of code to my Main class which goes as follows:
System.out.println("====================================================================");
System.out.println("JVM arguments:");
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> jvmArgs = runtimeMxBean.getInputArguments();
for (String arg : jvmArgs) {
System.out.println(arg);
}
// Print actual heap size used by the JVM
System.out.println("Max Memory: " + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + " MB");
// Your application code goes here
System.out.println("====================================================================");
long maxMemory = rutime.maxMemory();
System.out.println("Max Memory: " + maxMemory / (1024 * 1024) + " MB");
This code displays jvm Arguments and of course actual max memory to be used by JAVA on my system. It is initially set at 1896 MB. Output:
====================================================================
JVM arguments:
-XX:+ShowCodeDetailsInExceptionMessages
Max Memory: 1896 MB
====================================================================
Max Memory: 1896 MB
I tried adding the JAVA-OPTS variable to the system. Nothing changed after re-executing.
./././.>"C:\Program Files\Java\jdk-15.0.2\bin\java.exe" -Xmx2G -cp <path_to_my_main_class> Main
tried executing from terminal and specifying the Xmx value, and my Main class weren't recognized, even though I multiple-checked syntax, path and class name, same thing on cmd and vscode terminal (windows 11).
And still when I execute under VScode the main class runs correctly before returning the java.lang.outOfMemoryerror.
also I noticed that I had two different version of java on my pc by executing the command:
>where java
C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
while VSCode execution starts this way:
PS D:\Studies\M1\S2\TPs\méth\Projet Méta> d:; cd 'd:\Studies\M1\S2\TPs\méth\Projet Méta'; & 'C:\Program Files\Java\jdk-15.0.2\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'C:\Users\wahee\AppData\Roaming\Code\User\workspaceStorage\bdc4f5af61bc4e992a3808de6bdc92e9\redhat.java\jdt_ws\Projet Méta_de071d7c\bin' 'Main'
seems to specify another java instance. that I added to path system variable, and still nothing (made sure to constantly restart my PC)
I browsed stack overflow, google, and pushed chatGPT to his limits to a point it's just keeps apologizing and repeating steps over and over again...
I also accessed the control panel to add -Xmx and -Xms arguments within a java interface using a specific dialogue box with no change.
I am running on a x64 architecture. What Am I missing in all this? Or should I try something else?