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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question regarding stack and heap memory 2

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

A quick question.
When I create a static final primitive inside an Object, does the JVM saves it in the stack memory (since it is constant, therefore will never grow) or in the heap memory (where it saves all the Objects)?
 
For two reasons:
1. I am curious
2. I need to know it for a certain Java test I am taking...
Is that worry enough...? :)
 
You could try to write a small test programm, which declares an static final array of doubles, and increase it's size, until you get a stack- or heap-overflow error, and look which.

Code:
 public class StaFinTest
{
	private static final double [] da;

	static 
	{
		da = new double [1024*1024*64];
	}

	public static void main (String args[])
	{
		new StaFinTest ();
	}
}

> java -Xmx1024M StaFinTest
> java -Xmx512M StaFinTest
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

So the answer of first order is: heap
The answer of second order is: Try it out!

If you think arrays of primitives and primitives are handled differently, you could try to write a code-generator, which produces a program with 1024 doubles, and generate 64*1024 different classes of that kind - the size of the sourcecode allowed in a file is somewhat limited - too limited to put them all into one file.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top