I was experimenting with Vista today, specifically memory management and how it reacts in extremely depleted memory situations. As a quick and dirty simulation, I wrote the following vb script to just consume as much memory until an error occurs, then exit gracefully.
Code:
on error resume next
dim alloc(32676)
for n = 0 To 32676
alloc(n) = space(102400)
if err.number > 0 then
exit For
end If
next
This is basically doing exactly as those "memory defrag" programs are doing; forcing Windows to dump any cache and flush what it can to the page file. Once done, it exists and frees itself, magically giving you more free memory – but you’re then faced with a sluggish system that has to fetch everything from the page file.
It occurred to me that it's a good way to test if your system is starving for RAM. If you can run the script and don’t notice anything different (even clicking on the start menu, or switching to another minimised program should be a chore), your system is swapping too much and needs more RAM.
To run the script, just copy it to notepad and save it with a .vbs extension (you might need the hide known file extensions option disabled in folder options). The icon should be a blue scroll, provided you’re using Windows 2000 or later, or using Windows ME/9x and have the Visual Basic Runtime and Windows Script Hosting installed. Double click on it. If you have a lot of RAM or a slow CPU, it could take a while. If you’re using Windows 2000 or later, you should be able to see your available physical memory get sucked away to nothing and the size of your page file grow very large from the task manager. When it’s done chewing everything away, it will just stop and close. Your physical memory will return to normal very rapidly, probably higher than it was. Then you’re faced with the laborious swapping process, as the system turns to molasses when you try and do something. If you have enough memory, everything will load back into RAM as you use it, and it will eventually be as if you never did this torture.
For smarter Windows 2000 and later, this does nothing for you. In Windows 98 and earlier it sort of help programs load faster and may even keep them running for longer without a reboot, but I doubt it really. It would probably blue screen Windows ME.
Adding the following line to the end will prevent the script from exiting as soon as it’s done, and thusly not freeing the memory until you click on the Ok box. This is a fun experiment on memory usage for anyone with 1GB or greater.
Code:
msgbox "allocated. Click ok to free"