I know this question has been answered before for Eclipse but can someone tell me how to increase heap space of my JCreator for a program in a bit more detailed manner (I am a beginner to Java programming).
Note: using -Xms248m -Xmx3072m in cmd.exe did not work
I had written
java -Xms256m -Xmx3072m MyClassName
The error occurred when i was making a large array to make a prime number seive up to a billion numbers.
I have 4GB of ram.
public class MyClassName {
public static void main(String[] args) {
int a[] = new int[1000000000];
}
}
Related
My maths says the following Java program would need approx 8GB (2147483645 * 4 bytes) of RAM:
package foo;
public class Foo {
public static void main(String[] args) throws Exception {
int[] arr = new int[Integer.MAX_VALUE-2];
Thread.sleep(500000L);
}
}
This is backed up by observing the program when running:
But unless you set the max heap to around 12.5GB, the program fails to start:
$ java -Xmx12000m -cp ./ foo.Foo
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at foo.Foo.main(Foo.java:5)
$ java -Xmx12500m -cp ./ foo.Foo
//this works
Can understand the need for a bit of wiggle-room but why do we need so much?
Its because of the MinHeapFreeRatio default value (which is 40%). If you want to "need" less then you have to specify it: e.g. 5%
-XX:MinHeapFreeRatio=5
Also, you need to change memory allocated to the young generation as it plays a important role in memory allocation:
After total available memory, the second most influential factor affecting garbage collection performance is the proportion of the heap dedicated to the young generation.
try this:
java -Xmx9g -XX:MinHeapFreeRatio=1 -XX:MaxNewSize=256m -cp ./ foo.Foo
https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/sizing.html
recently I am doing an experiment on JVM and bytecode.
I use these code snippets to test.
import java.util.*;
public class Simple {
private String a = "abcdefghijklmnopaqrstuvwaxyazaaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyaz";
public int test()
{
String bb = "abcdefghijklmnopaqrstuvwaxyazaaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyaz";
int a = 0;
int b = a;
int c = a + b;
return c;
}
public static void main(String[] args)
{
String cc = "abcdefghijklmnopaqrstuvwaxyazaaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyazaabcdefghijklmnopaqrstuvwaxyaz";
Simple simple = new Simple();
simple.test();
Scanner input=new Scanner(System.in);
System.out.println("how much money do you need?");
double number=input.nextDouble();
}
}
FIrstly I use HotSpot to conduct the experiment. On Windows, I trun off the
-Djava.compiler=NONE
and use HeapMemView to view the HotSpot's heap memory. I can find a sequence of "6162 6364.."(whichs match my private String variant) and find my code snippet's bytecode sequence.
But I cannot find the bytecode sequence of Java Standard library.. like
Java.Lang.Obeject
Java.Lang.Math
What's wrong..? In my understanding, I think I should find their bytecode sequence in the JVM's heap..
Then I use JRocket to do it again.. use
-Djava.compiler=NONE
to turn of the complier mode... but this time I cannot even find my String variant on the heap....
I am trapped here for two days.. Could anybody can me some help...? I really really appreciate it...
Thank you!
I am trapped here for two days.. Could anybody can me some help...? I really really appreciate it...
I would focus on the problem you are trying to solve first. Perhaps you could make it clearer as to why you are doing this in the question.
On Windows, I trun off the -Djava.compiler=NONE
This only changes how the code is compiled to native code. This will not change the heap in any way.
But I cannot find the bytecode sequence of Java Standard library.. like
The byte code and class definitions are not in the heap, they are in the perm gen.
When i run the below program i got the exception when for loop begins its execution at i=1031521. How to over come memory usage of this type?
class wwww
{
public static void main(String args[])
{
String abc[]=new String[4194304];
String wwf="";
int s_count=524286;
for(int i=0;i<4194304;i++)
{
System.out.println("----------enter--------"+i);
abc[i]=""+i;
System.out.println("----------exit--------"+i);
}
}
}
The exception is:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.
java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390
)
at java.lang.StringBuilder.append(StringBuilder.java:119)
at wwww.main(wwww.java:12)
This is because your your uses up all the heap space allocated to your jvm.
You can use argument while running the program to specify the heap size that you would like to allocate.
This is an example:
java -Xmx256m MyClass
Here a maximum of 256 MB of heap space will be allocated
How to over come memory usage of this type?
Don't perform memory usage of this type. You are creating 4194304 strings, of the general form ""+i. You don't need 4194304 strings of that form all at once. You only need one of them at a time, if any, and you can create it every time you need it.
You could either:
Increase the heap size that you give to your program. This is done via the -Xmx command-line argument to java.
Re-engineer the program to use less memory. Do you really need to keep all those strings in memory at once?
I knowingly created the following class to cause out of memory error
public class Test1
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
while(true)
{
Test1 a = new Test1();
sb.append(a.toString());
}
}
}
As I expected this above class fails with what I wanted...
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
at Test1.main(Test1.java:10)
but this:
public class Test1
{
public static void main(String[] args)
{
StringBuffer sb = new StringBuffer();
while(true)
{
Test1 a = new Test1();
System.out.println(sb.toString());
sb.append(a.toString());
}
}
}
Does not crash. Runs just fine, by printing the object address over and over again on console.
My question is:
What difference a simple SOP made?
Your assumption that there is no OutOfMemoryError is likely to be incorrect. It is just massively delayed. Printing a string that is getting bigger and bigger on the out stream takes so much time, that your loop may take an hour to run out of memory.
You can double-check this, by printing only every 10th, 100th, 1000th time. You'll see the error will occur the earlier the less IO you generate. Probably you'll see a curve like this in jconsole:
As you can see, the heap is slowly but steadily going up. Even if I try to force garbage collection (15:02 and 15:07), I cannot free all memory anymore. But since I'm still only at 5% of my heap, I'll stop running your code now :-)
Just tested a bit, the actual bottleneck is sb.toString(). This of course takes time proportional to the length of the string(buffer), so every next loop takes a tiny bit longer to execute. Before you run out of memory, after a few thousands loops, one loop will take a couple of seconds just to create the string.
Replacing sb.toString() by a long counter, makes it crash 'instantly' aswell. Removing the System.out.println() has little effect on speed.
On my computer java -Xmx2m Test1 > /dev/null takes about 8 minutes to crash. With a normal heap size this could take days. (Feel free to try it.)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
im running a simple loop that prints out the iterator (i) for 1.000.000 times in both java and c.
im using netbeans and visual studio respectively.
i dont care about precision but at about 40 seconds:
netbeans (java) has printed about 500.000 numbers
while windows (c) has printed about 75.000 numbers
-- why such a big difference?
im using a common intel core2duo(2.0 Ghz) pc with windows7
That seems wrong. Could you provide your code?
My Versions:
C version compiled with gcc -std=c99 -o itr itr.c with gcc 4.5.1
#include <stdio.h>
int main( int argc, char **argv )
{
for ( int i = 0; i < 1000000; i++ )
{
printf("%d\n", i);
}
}
Java Version compiled as javac Itr.java with javac 1.6.0_20 and JVM being:
OpenJDK Runtime Environment (IcedTea6 1.9.1) (ArchLinux-6.b20_1.9.1-1-x86_64)
OpenJDK 64-Bit Server VM (build 17.0-b16, mixed mode)
code -
class Itr
{
public static void main( String[] av )
{
for ( int i = 0; i < 1000000; i++ )
{
System.out.println(i);
}
}
}
and the times:
time ./itr
// Snip Output //
real 0m1.964s
user 0m0.330s
sys 0m1.477s
time java Itr
// Snip Output //
real 0m5.245s
user 0m2.337s
sys 0m3.023s
The test system is a Intel Core i5 M520 ( # 2.4GHz ) running 64 bit ArchLinux.
One way to considerably speed up your example would be:
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000000; i++)
sb.append(i).append("\n");
System.out.println(sb.toString());
}
String concatenation or output (in your case printing to standard output stream) in a loop is bad by design and not the fault of Java, you just generally want to avoid that.
It is much faster if you minimize the calls to output and use a local buffer. Also concatenating Strings is also inefficient - Java has StringBuilder class for that task.
Without providing your code and environnement settings, your test have no value.
Are you sure that the NetBeans console display isn't slown down in C case, or optimized for Java output?
Are you sure you did run the two projects in optimized mode without debug? C debug versions often generate a lot of debug informations that clearly slow down everything if you're debugging. Anyway, any benchmark should be done with optimization AND no debug mode.