Memory management under Windows The Need for Memory:- The window environment and most applications that run under it need access to phenomenal amounts of memory. Multitasking is part of the reason: system must have ability to load and execute several large applications simultaneously. The GUI too calls for increased amounts of memory. Bitmaps, icons, cursors: they all need much larger amount of memory then the simple screen elements of text-based systems.
The operating system itself primarily exists as a collection of dynamic-link libraries, which must be loaded into memory to provide support to executing applications, as must the various device drivers which allow access to hardware as essential as the video card, the mouse, and the keyboard. It is imperative for windows to access and efficiently manage huge amount of memory if everything is to work together as a speedy and usable environment.
Memory Handles and Locks The most computers running Windows do have large amounts of memory physically installed on them, windows memory management must seem like a pretty straightforward chore. In fact, things are pretty simple when the system starts up. A single huge amount of memory, almost limitless in size, is available. All that’s needed is a system that curves out small chunks from this as and when required, and parcels them out as users load up more applications. As soon as an applications terminates, all the memory earmarked launched by the user. This seems good enough on paper, but things turns out very differently in practice. As a programs allocates and discard memory blocks of various size, a stage arrives where most of the memory available in the system gets broken up into tiny piece of intermingled free and allocated memory blocks.
As a result of this, the system fails to load fresh applications because, even though enough memory is available, no single block of memory is large enough to accommodate the new application’s code. In fact, the system behave exactly as if it had run out of memory, and the presence of large amounts of physical memory doesn’t achieve much. This “fragmentation” of memory under multitasking system is a well-known phenomenon, and the consolidation of widely Scattered chunks of free memory is central to the windows memory management scheme.
Solution for the fragmentation problem It must be clear that memory movement holds the key to the memory fragmentation problem. The operating system must devise some through which it can be safely coalesce several smaller free blocks together into a single large one by moving out the intervening could cause severe problems. A program would be allocate memory, and the system would move it away to a different location later, effectively pulling the rug from under the application program’s feet.
Solution:-Concept of Locking Window uses the concept of memory handles as a solution. Whenever programs request memory, a memory block is allocated, and a memory handle is returned to the program instead of the block’s address. Whenever applications programs need to access to access the memory block, they can “lock” the block using a special window API function. Window make sure that the specified block was not moved during the compaction process, and also return the block’s current physical address for the program’s use.
After finishing its processing tasks, the program can use another API function to “unlock” the memory block, once again allowing windows to move this block whenever required. Problem Persist:- Though locking did provide an answer to the fragmentation problem. Many problem still remained. Application program could not leave memory blocks locked in place for a long time, because even a single unmovable block would be enough to negate windows’ memory compaction efforts. In fact, programmers had to continuously on their toes to unlock every memory block as quickly as possible.
The effect of premature unlocking had its own perils: Windows would move the block, while the program would continue accessing it using the old address, the program would most likely crash. A special testing programs called “shakers” were used to detect these bugs. These worked by forcing windows to move memory blocks around, in an attempt to expose smoke out any hidden glitches.
The Memory API Windows provides a large set of memory management functions, which provides a fairly low level of control over how memory management and protected mode selector management:- 1.GlobalLRUNewest():- marks a global memory block as the most recently used block, making it the least favored block for discarding by windows. 2.GetSelectorBase():- looks up the 80x86 descriptor table and retrieves the base physical address associated with a particular selector.
List of functions:-used for allocating and managing global memory blocks 1.GlobalAlloc() 2.GlobalFree() 3.GlobalReAlloc() 4.GlobalLock()
1.The GlobalAlloc() function is used for allocating a memory block of any desired size. We pass two parameters: allocations flags and allocations size. Allocations flags control how windows satisfies the allocations request. Syntex :- GlobalAlloc(UINT fAlloc, DWORD cbAlloc) The cbAlloc parameter contains the requested memory size. The maximum allowed values depend on the microprocessor that’s being used: mb- 80 bytes mb- 64 kb
The GlobalAlloc() function returns a handle to the allocated memory block, which has the data type HGLOBAL. 2. GlobalFree() performs the reverse actions, by freeing the given memory block after we have finished using it. The memory block must not be locked when GlobalFree() is called. Syntax:- HGLOBAL GlobalFree(HGLOBAL hglb) We pass single parameterha, the global memory handle which is to be freed. 3. GlobalReAlloc():- it can be used to change the block’s size even after the block has been allocated and loaded up with data. It can also be used to change the block’s allocations flags at any time.
Syntax :- HGLOBAL GlobalReAlloc(HGLOBAL hglb, DWORD cbAlloc, UINT fAlloc) Three parameter are required: memory handle, new allocation size, and allocation flags. 4. The GlobalLock():- functions locks up the given block, preventing windows from moving and discarding it, and also return a FAR pointer containing the physical address. It is possible to lock the same block more than once, because window maintains a “lock count” for each block. Syntax:- Void FAR *GlobalLock(HGLOBAL hglb)
The Kernel Macros We can lock all our memory blocks, so we should eliminate all the monotony of repeated sequences. Windows provides a set of three very useful macros for this purpose:- Void FAR *GlobalAllocptr( UINT fAlloc, DWORD cbAlloc) Bool GlobalFreeptr(Void FAR *lp) Void Far *GlobalReAllocptr(void FAR *lp, DWORD cbAlloc, UINT fAllocate)
These memory allow us to work directly with far memory pointers, making it unneccessary for us to even save the windows memory blocks.