Thursday, May 01, 2008

Sarah Cynthia Sylvia Stout did not take the garbage out!

Deadlines are becoming outrageously tight and developers are left as last men standing. Products run through numerous loops of testing just to crash at architecture levels. Time crunch has become a major issue that most of us face and find hard to handle. But, this isn't an excuse for poor coding and improper usages. We can't call ourselves as "Programmers", if we don't know to code clean and clean after coding.

In the Flash World, a lot of programmers blame the Player for not doing its work when needed, but, if you take a look at the code, you will find a lot of misses – onEnterFrame not being deleted after use, Intervals not being clears, typing not done for variables, having insanely numerous variables – a lot of which don't get used..and the list goes on.

The Flash Player has evolved with time and become a lot better when it comes to memory management and garbage disposal. The built-in garbage collector removes unused objects from the memory, enabling objects-in-use to use the memory. The Flash Player 9 adopts the strategy of marking objects having active references. Objects that aren't marked are automatically considered inactive and removed from memory when the cleaning cycle runs. Yeah, it's a "cleaning cycle", which means that inactive objects are not removed from the memory immediately, but, they are removed when the next cycle runs. There seems to be no way of knowing when this cycle runs. Hence, code needs to be written in a way that makes sure that memory is managed efficiently by-

  • Removing objects and references to objects that are not in use.
  • Using memory monitoring code. We have the handy System.totalMemory to its advantages. When I suggested using this to a friend, I found every function in a class having an IF statement checking memory usages. Man!
  • Only program for what is needed. I've seen programmers implement what they call "Flexibility" to their architectures. This "flexibility" is just code that lies waiting till the real feature is brought in after 20 years and 20 versions, whichever comes first!
  • Run a code clean up cycle after getting in all the functionality. It helps you relook at the code and make improvements. There would have been things that could be done in a different way.
  • Resource hungry code needs to have clean ups like – onEnterFrames need to be deleted, objects should be nulled after use.

UPDATE: I found this article on Devnet - http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html. This explains things in a better way!

Happy Coding!


 

No comments: