> > 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? >
I am no java expert (don't really care for java and don't have a current need to use it) but when an object goes out of scope it's marked as eligible for garbage collection. There is no guarantee for when that will happen and is totally up to the VM to do it. You can manually run the garbage collector but it's a bad practice to get into because it will do a full collection and is slow. > 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. > > ... 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. > If you are worrying about design and readability then this doesn't sound like code that is going to be thrown away, in which case i would do it "the right way". I don't know what the culture is like where you work, but i would be really careful of getting something working that you think is hacky prototype code just to have your boss tell you it's good enough and you should move on to the next thing. If you are sure that it's just a proof of concept, that the results are all the matter, and no one but you will ever look at the code, then don't worry about readability and reuse the objects the way you are then throw the code away when you're done and do it right. Otherwise throw it away now and do it right.
-------------------- 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
