What is scoped memory and why java uses it?
August 27, 2010 Leave a comment
In java, a frequent occurence of a common phenomenon related to memory management is creation and destruction of temporary objects. What are java temporary objects? For example in java, when two strings are concatenated, a StringBuffer object is created. As two Java String objects cannot be added directly (immutable property), a helper object StringBuffer is used to construct a resultant java String object. This StringBuffer object is a temporary object, which is of no direct interest to the programmer. Most of the programming constructs are built in this type of model. Result of it is there are lot of garbage left behind.
Now the question is, what happens to the temporary java object and such garbage after the intended operation completes? Will it be saved for the life time of the program, or JVM (java virtual machine) or what is the underlying policy to destroy it? In automatic memory management the hassles of allocating and deallocating memory is no more a part of programmer’s job in java. But the downside of it is you never know, when an object will be freed. The java garbage collector (GC) doesnot provide any defined time boundaries.
This is where the java scoped memory concept comes into picture. Scoped memory concept is declaring to the JVM that, I am going to use this much amount of memory and those can be freed after this period. That is defining a boundary and bringing predictability. This predictability is needed the most for real time programming. Where you need to be certain about the cycles, time and cost of the operation.
In java scoped memory, the memory is preallocated for the task. This will ensure that the program will not get struck in the mid of operation because of memory resource shortage and constraints. Memory is deallocated and freed when the associated task gets completed. Therefore the recycling process of the memory is fast. But, all the objects related to that task and comes under that designated scope will disappear. If some objects are needed for future reference then that particular object needs to be associated to a different memory mechanism.
This is one of the latest feature in java adding power to the real-time java implementation. For more detail look into the Sun Java Real-Time System (Java RTS). It is Sun’s commercial implementation of the Real-Time Specification for Java (JSR-001).