This may be a REALLY obvious question but....

I am not very familiar with Java aside from the few projects they had us do
in Java back at BYU... but I am writing in Java currently because that is
what the people around me know.

Anyway, I was writing some quick and dirty idea prototype code, and I had a
bunch of nested loops, and I had to create some objects inside the loops,
and then, theoretically, they should get garbage collected automatically
right? Anyway, because the loops were running fast enough, I assume that
object creation got ahead of garbage collection, and I got:

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

Now, I know that object creation is something you should move outside loops,
for speed reasons, but this was quick and dirty prototype code, and speed
wasn't really an issue.

What I eventually did was I moved the object creation outside the loops, and
wrote a new method (which I called reset) that did what the constructor used
to do, but took the existing object, and reset it with new values. In this
way I wasn't creating new objects each time, and it solved the problem.
However, from a design and readability standpoint, that seemed a bit clunky.
Is there another way to deal with this? If I was in C, I would just destroy
the objects at the bottom of the loop, and walla, problem solved, but I
can't do that in Java.

Thanks,

James

-- 
Web: http://james.jlcarroll.net
--------------------
BYU Unix Users Group 
http://uug.byu.edu/ 

The opinions expressed in this message are the responsibility of their
author.  They are not endorsed by BYU, the BYU CS Department or BYU-UUG. 
___________________________________________________________________
List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list

Reply via email to