how to solve this problem : out of memory

B

Baby Lion

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I think it's because I have big ArrayList ,
is there any way to prevent it ?
 
V

Vishu

... setting the arraylist's reference to null when you are done using
it..will alteast help the GC do its magic..; more than that.. things
you might be usign some loops.. or other stuff.. maybe more info will
help..
 
S

su_dang

Baby said:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I think it's because I have big ArrayList ,
is there any way to prevent it ?

You can try to increase your heap space by adding this

-Xmx1024m

to your command line when starting up java such as

java -Xmx1024m YourClass
 
B

Baby Lion

thank all of you for helping me

and how to set the eviroment in eclipse to increase the heap?
(e-mail address removed) 写é“:
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Vishu said:
.. setting the arraylist's reference to null when you are done using
it..will alteast help the GC do its magic..;

In general it is considered bad practice to explicit
set to null.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Tim said:
Wonderful! - back to the days of the user hand-tuning memory allocation -
remember config.sys!!

Clearly memory management is far to important to be left to the computer.

It is an requirement for serious systems to be able to
divide resources between applications.

It is not memory management in the classic sense of
the word.

It is protecting other applications on the system.

Arne
 
G

Guest

Baby said:
and how to set the eviroment in eclipse to increase the heap?
(e-mail address removed) 写é“:

You specify it as a JVM parameter in the run config.

Arne
 
P

Patricia Shanahan

Arne said:
It clutters code and does not improve performance
(in 99% of cases).

I am not the right person to explain GC.

But read:

Effective Java / Bloch - item 5.

or read the long thread at:

http://www.theserverside.com/news/thread.tss?thread_id=36362

It is worth noting that there are special cases where it do
improve performance, but those are the exceptions.

It is indeed bad practice to add ANY statement to a program unless it is
going to do some good.

I don't see why assigning null to a reference variable should be singled
out as being bad practice, rather that just falling under the general
rule of avoiding useless code. I have found it to be an effective
technique for reducing memory footprint under the following conditions:

1. The reference variable or array element itself has a long lifetime.
Don't do it for a local variable just before the method containing it
returns.

2. It may be the last reference to the data structure. The payoff is for
making some object or objects unreachable, so that GC can take them away.

3. The data structure is large enough to care about.

Patricia
 
A

Andrew Thompson

Baby said:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I think it's because I have big ArrayList ,

Just how 'big'? How many elements of what?

Note that the JVM can handle some pretty large structures
in the standard memory, and if this is indeed a memory
*leak*, rather than simply caused by a large data structure,
increasing memory will merely delay the OOM Error,
not prevent it.

Or, to put that another way, it might be a good idea
to *make* *sure* that the ArrayList is causing the
memory problem.

...Just a thought.

Andrew T.
 
C

Chris Uppal

Arne said:
It is an requirement for serious systems to be able to
divide resources between applications.

But it's not a requirement (of the OS) that the user should have to tell it how
much memory the app is going to use in advance. The JVM's inflexibility in
this is somewhat puzzling -- I have no other implementations of GC-ed languages
which share this odd requirement[*].

-- chris

([*] Well, actually I do have one, but it's an implementation of the function
programming language SASL, which was written around 1976....)
 
C

Chris Uppal

Patricia said:
It is indeed bad practice to add ANY statement to a program unless it is
going to do some good.

I don't see why assigning null to a reference variable should be singled
out as being bad practice, rather that just falling under the general
rule of avoiding useless code.

I think the only reason for singling it out was that it was the only example
of bad practise under discussion.

("bad practise" only in the sense previously discussed, of course.)

-- chris
 
L

Lew

Chris said:
But it's not a requirement (of the OS) that the user should have to tell it how
much memory the app is going to use in advance. The JVM's inflexibility in
this is somewhat puzzling -- I have no other implementations of GC-ed languages
which share this odd requirement[*].

I have watched the 1.4 JVM grab memory until it uses well over 1GB, absent
setting a limit with -Xmx. That indicates to me that the JVM will take *all*
available memory by default. The only "inflexibility" therefore would be that
the JVM allows one to set a cap when failing to do so might be dangerous.

I haven't Googled (Yahooed, Asked, ...) yet to be sure I'm interpreting my
observations correctly, but it sure looks like the JVM does not require a max
heap allocation, but it does permit one. Pretty much the antithesis of
inflexibility.

- Lew
 
C

Chris Uppal

Lew wrote:

[me:]
But it's not a requirement (of the OS) that the user should have to
tell it how much memory the app is going to use in advance. The JVM's
inflexibility in this is somewhat puzzling -- I have no other
implementations of GC-ed languages which share this odd requirement[*].

I have watched the 1.4 JVM grab memory until it uses well over 1GB, absent
setting a limit with -Xmx. That indicates to me that the JVM will take
*all* available memory by default.

If it did work like that then it would never be possible cure an out-of-memory
condition by setting an explicit max -- only to create one.

BTW, the amount of memory the OS reports the JVM to be using is not
particularly closely tied to how much it is actually using (and especially not
to how much it is using for actual objects), and is even less tightly tied to
the min/max parameters (whether the defaults or set explicitly).

-- chris
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Patricia said:
It is indeed bad practice to add ANY statement to a program unless it is
going to do some good.

I don't see why assigning null to a reference variable should be singled
out as being bad practice, rather that just falling under the general
rule of avoiding useless code.

It is not so much the bad side of the practice that single
it out but more the non existing good side.

Setting to null do have an effect on systems using reference
counting. Like COM. But Java does not do reference counting.
I have found it to be an effective
technique for reducing memory footprint under the following conditions:

1. The reference variable or array element itself has a long lifetime.
Don't do it for a local variable just before the method containing it
returns.

2. It may be the last reference to the data structure. The payoff is for
making some object or objects unreachable, so that GC can take them away.

3. The data structure is large enough to care about.

That is one of the examples in the long TSS thread.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Chris said:
Arne said:
It is an requirement for serious systems to be able to
divide resources between applications.

But it's not a requirement (of the OS) that the user should have to tell it how
much memory the app is going to use in advance. The JVM's inflexibility in
this is somewhat puzzling -- I have no other implementations of GC-ed languages
which share this odd requirement[*].

It is common to see restrictions on memory usage per
process enforced by the OS.

I see this as the Java equivalent.

Arne
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Lew said:
I have watched the 1.4 JVM grab memory until it uses well over 1GB,
absent setting a limit with -Xmx. That indicates to me that the JVM
will take *all* available memory by default. The only "inflexibility"
therefore would be that the JVM allows one to set a cap when failing to
do so might be dangerous.

I haven't Googled (Yahooed, Asked, ...) yet to be sure I'm interpreting
my observations correctly, but it sure looks like the JVM does not
require a max heap allocation, but it does permit one. Pretty much the
antithesis of inflexibility.

What Java & OS version ?

http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/java.html

says:

-Xmxn
Specify the maximum size, in bytes, of the memory allocation pool.
This value must a multiple of 1024 greater than 2MB. Append the letter k
or K to indicate kilobytes, or m or M to indicate megabytes. The default
value is 64MB. Examples:

-Xmx83886080
-Xmx81920k
-Xmx80m

And according to my experience it is correct !

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top