On Fri, Sep 24, 2010 at 4:38 PM, James Carroll <[email protected]> wrote:
> 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. > ~disclaimer I have not recently touched anything java~ Well since this is quick and dirty prototype code one of the first things that comes to mind is actually just increasing the heap size. http://hausheer.osola.com/docs/5 There is also a call you can make to get the garbage collection to run. I think it is just a suggestion and of course you would have to do it after the object went out of scope so slightly different place than where you would free it in c. http://download.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#gc() Although to be fair I kinda like the idea of reseting and just continuing to use the same object. Daniel Rich
-------------------- 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
