Keep RAM defragmented in windows

Share your advanced PureBasic knowledge/code with the community.
User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Keep RAM defragmented in windows

Post by Psychophanta »

Windows NT, 2000, XP, 2003 ... 10, have the problem of fragmented memory and those oses are not able to manage it in an optimal way.
There are a small trick to defrag the RAM with a simple command.

This tip is to keep the physical RAM defragmented time to time.
I just use it in the Start menu->Start folder, and the results are nice, over all if the computer is turned-on during more than 48 hours without restart system neither switch-off it.

It seems to work for XP and older, and old PC (pentium 4 or olders).

Code: Select all

; What this code does is it allocates large chunk of memory in RAM and If allocated size is larger than available free RAM then this forces OS to swap some less used
; RAM pages to disk (pagefile) to make space for newly allocated memory.
; This code (vbe script) does nothing else and it ends immediately so this newly allocated memory is also immediately marked as free RAM.
; But swapped memory pages from pagefile are not loaded back to RAM immediately now that there is free space for them again, they are loaded back only when these apps
; which allocated these swapped out pages try to access them again in the future.
; And only then, in the future, "RAM defragmentation" happens because OS tries To put pages from pagefile to RAM in sequential order as much as possible.
; 100% defragmentation is not guaranteed and practically impossible.
; It is also theoretically possible to sometimes end up with more fragmented RAM by running this code.
; 
; Whole explanation above is obviously simplified and there is a lot more going on "under the hood":
; https://msdn.microsoft.com/en-us/library/ms810627.aspx
; https://en.wikipedia.org/wiki/Paging

ram0.MEMORYSTATUSEX:mem.l:minutos.u=9
ram0\dwLength=SizeOf(MEMORYSTATUSEX)
GlobalMemoryStatusEx_(@ram0)
ram.i=ram0\ullTotalPhys/1024/1024
mem=ram*0.6
Repeat
  Delay(minutos*60000)
  OpenFile(0,"ram.vbe",#PB_Ascii):;TruncateFile(0)
  WriteStringN(0,"Mystring=("+Str(mem)+"000000)",#PB_Ascii)
  CloseFile(0)
  If RunProgram("ram.vbe","","",#PB_Program_Wait):DeleteFile("ram.vbe")
  Else:Beep_(700,7000)
  EndIf
ForEver
Notice the variable 'minutos' is rate to perform the thing.
Last edited by Psychophanta on Sun Dec 31, 2017 5:45 pm, edited 1 time in total.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Keep RAM defragmented in windows

Post by cas »

What this code does is it allocates large chunk of memory in RAM and if allocated size is larger than available free RAM then this forces OS to swap some less used RAM pages to disk (pagefile) to make space for newly allocated memory. This code (vbe script) does nothing else and it ends immediately so this newly allocated memory is also immediately marked as free RAM. But swapped memory pages from pagefile are not loaded back to RAM immediately now that there is free space for them again, they are loaded back only when these apps which allocated these swapped out pages try to access them again in the future. And only then, in the future, "RAM defragmentation" happens because OS tries to put pages from pagefile to RAM in sequential order as much as possible. 100% defragmentation is not guaranteed and practically impossible. It is also theoretically possible to sometimes end up with more fragmented RAM by running this code.

My whole explanation above is obviously simplified and there is a lot more going on "under the hood":
https://msdn.microsoft.com/en-us/library/ms810627.aspx
https://en.wikipedia.org/wiki/Paging

And here are some problems with this code:

[*] in the last two Select cases you probably meant to write Case 131072 and Case 262144 for people with 128GB and 256GB of RAM. But this whole Select : EndSelect is useless, as explained below...

[*] since you use GlobalMemoryStatus_(), every case after 2048 is not needed because it will never happen (this code here on my system sets ram.l variable to 2047 but i have 16GB RAM, not 2GB as it reports), you need to use GlobalMemoryStatusEx_() which returns correct values for systems with >2GB:

Code: Select all

ram0.MEMORYSTATUSEX
ram0\dwLength=SizeOf(MEMORYSTATUSEX)
GlobalMemoryStatusEx_(@ram0)
ram.i=ram0\ullTotalPhys/1024/1024
[*] BIOS shadowing is enabled on most systems (it can be disabled but it is usually enabled by default) and you will get for example 511 instead of 512, 2047 instead of 2048, or 8191 instead of 8192 so your predefined Select cases will be almost always skipped and it will go to Default case. iGPU enabled systems make this even worse. On my PC with 16GB of RAM but with enabled iGPU which reserves 512MB of RAM for itself, ram variable is set to 15818 and looking by Select cases, mem variable should be set to something around 2500 to be in line with other cases, but it goes to Default case which sets this variable to 9886 (15818*80/128).

So, whole Select : EndSelect part is pretty much useless, it is better to replace it with just a single line, something similar to this:

Code: Select all

mem=ram*0.6
Also, if you have SSD and not much free RAM then this will wear out SSD quicker. If you have decent amount of RAM then you do not need this "RAM defragmentation". I only reboot my PC when i am forced to do it by windows update so i regularly reach 20-30 days of uptime and i never had any need for "RAM defragmentation".
User avatar
Psychophanta
Addict
Addict
Posts: 4975
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

Re: Keep RAM defragmented in windows

Post by Psychophanta »

Thank you very much cas.

I forgot to say the tip is for XP or older machines with pentium 4 or older and 2 GB or less.

It works for me now that i am at my parents home, where i have a Pentium 4 with 1.5GB :)

Modified first post.
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen:
Post Reply