Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OutOfMemory Heap Error

Status
Not open for further replies.

saadwharton

Programmer
Aug 8, 2009
6
US

I have a Java program with a class. The class has a private data member which is an array of integers. The array has 4 million data members. I initialize all 4 million data members with 0. When initializing, the program runs out of Heap memory when the loop iteration gets close to the 1 millionth data item.

Here is the class code:

class MyClass {

private:
int [] a ;

public:
MyClass() {
a = new int[4000000] ;

for (int i=0;i<4000000;i++) {
a = 0 ;
}
}
}

I tried this on a machine running Windows Vista with 1 GB of DDR RAM. The IDE is Eclipse.

I found instructions to increase the amount of heap space available to Eclipse. Basically, I set the Xms and Xmx arguments to high numbers (512M and 1024 M) respectively. But this does not get rid of the error.

How can I work with these 4 million array items in the limited memory that I have? Why does the JVM not use Virtual Memory when it runs out of memory? Available virtual Memory is much larger than real memory - since its size is constrained by the amount of free disk space.


Thanks-in-advance to the person who helps with this.

Sincerely,


Saad
 

Sorry - it is like this:

class MyClass {

private int [] a ;

public MyClass() {
a = new int[4000000] ;

for (int i=0;i<4000000;i++) {
a = 0 ;
}
}
}
 
You don't have a main method, so how do you start it?

It's an excellent idea to reduce the problem to the bare minimum, and to just post the defective part, but you have to ensure that your example is still bogus. So you need a main method.
Code:
class MyClass
{
	private int[] a;

	public MyClass ()
	{
		a = new int[4000000];

		for (int i = 0; i < 4000000; i++)
		{
			a[i] = 0;
		}
	}

	public static void main (String... args)
	{
		new MyClass ();
	}
}
This will run, but doesn't reproduce the error. Well - I'm using linux, but but Vista is not thaaat bad.

All elements of an array are by default set to 0, so effectively you just need this:
Code:
class MyClass
{
	private int[] a = new int[4000000];

	public static void main (String... args)
	{
		new MyClass ();
	}
}
Maybe you somehow managed to initialize 4 million arrays of size 4 million?

don't visit my homepage:
 
You don't need to increase Eclipse heap size, but your progam's one, as it runs on a separate VM.

Put the flags on the options for Run

Cheers,
Dian
 

In Eclipse, under Project->Properties->Arguments tab->Program arguments and VM arguments, I set the following variables.

Xms128m
Xmx1024m

and compiled the program. The compiler did not like the above arguments so I took them out.


My main method is as follows:

public static void main(String[] args) {

Myclass a ;
int j = 0 ;

File thefile = new File("C:\\file.txt") ;
FileInputStream fs = null;
BufferedInputStream bs = null;
DataInputStream ds = null;
String line ;
String [] tokens ;
long numMisses = 0 ;
int k = 0;


a = new MyClass(32,1000000, 2) ; <-- the Java Heap Out of Memory error occurs in the MyClass constructor

}
 
You need so set the arguments at runtime, not compile time.

Rigth click the class -> Run As -> Run configurations

Cheers,
Dian
 
The main method is - I'm sorry - completely bogus.

You define half a dozen of variables (fs, bs, ds, thefile, line, token, numMisses and k) without ever using them. Why?

And if your Constructor throws an exception, it would be useful to show the constructor too.

don't visit my homepage:
 
stefanwagner,
This is not the complete code for the main method. I am using all of the variables later on in the main method. I posted a simplied version of the main method for objectivity to solving the problem I am having. I know that the problem has to do with the number of integers I am initializing in the array 'a'.

Can you please stop using the word 'bogus' in your posts? :)

I will also try Diancecht's suggestion.

Meanwhile, (note to self), it might be useful to email Sun's tech support for answers.

 
Email Sun with an Eclipse problem, that will be funny :)

If you're planning to contact Sun, execute your program from command line.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top