Please enable JavaScript to view this site.

ESL Documentation

Navigation: » No topics above this level «

Q13 How does memory becomes fragmented?

Scroll Prev Top Next More

Imagine the following example:

FAQ_MemoryManagement_Q13_Sample1

An ESL string is stored as a structure that holds some data about the string, as well as a "pointer" to the location of the string's value in memory.  In the "C" language, in which ESL is written, a pointer is a reference to the memory location at which the string starts; since ESL relies on the fact that each byte of the string is in the "next" continuous memory address, before copying a value to a string variable ESL must search its memory for the first available segment of memory large enough to hold the new value.  Since we are starting with a clean slate, imagine that after the response to start, ESL needs to find a one-byte location to hold the new value to be assigned to the variable Holder.  Since no memory has yet been allocated, ESL will allocate what is required (one byte), and copy the value into that memory location (let's call it Register 1), and update the variable structure so that it knows to look at Register 1 in order to find that value.

The next instruction causes ESL to do a similar thing.  In this case, the variable Fragmenter will have a value which requires three bytes of storage, and which starts at Register 2.

The final instruction is a little different, though.  Here, ESL is replacing the value of a variable.  ESL first frees the memory being used by the current value of the variable.  Next ESL begins to search through memory looking for a spot large enough to hold the new value (in this case a spot which is at least two contiguous bytes).  Since the first spot is not big enough, and Register 2 is currently in use, ESL will allocate another section of memory, 2 bytes this time, starting at Register 3.

At this point, we have an interesting problem:  we have used 6 bytes of memory to store 5 bytes of data.  Throughout the program, this little empty section of memory will never be used unless there is a need to hold a value that is only one byte long.  This condition is referred to as fragmented memory.  Imagine this situation occurring over and over again, throughout the day.

As an ESL developer, you should note that the minimum size of memory allocated is 8 bytes and all memory allocation have an even number of bytes to ensure the maximum re-use is made of all block that are no longer needed.

Or, imagine the case where new values of a string keep replacing old values, and the new values are slightly larger than the old values.  If the old value started at 20K (20480 bytes) and got bigger by 1K at a time, ESL would need to allocate an additional 20K (plus another 1K for each time this process repeated) at every new iteration.  It is not hard to see how, depending on the types of operations being performed, ESL could use up all the memory that it had been allocated.

Keep in mind that this exact same process is used by any statement in ESL which can cause it to consume memory, such as creating objects, adding contents to an object (for instance adding a line to a textual region), copying a value to a variable or array, resizing an array to be bigger, declaring or setting a string value from within a DLL which calls the EslCreateString or EslSetStringValue functions, etc. Deleting objects, clearing objects, copying a smaller value to a variable or resizing an

Array to be smaller can all take advantage of memory that has already been allocated, but invariably, ESL will not be able to efficiently use every little block of free memory, and the result will be memory fragmentation.  If permitted to continue unchecked, memory fragmentation can lead to an ESL application terminating abnormally.  To recover fragmented memory and avoid such an undesirable outcome, the developers of ESL have provided the necessary tool to solve this problem - the "squeeze memory" action statement.