Created
June 9, 2022 10:20
-
-
Save Nucs/9cbb1aa1a76ce5487b5d23dfd64fd7b8 to your computer and use it in GitHub Desktop.
Memory Inflating Tool used to allocate memory while preventing windows RAM compression and disk caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Runtime.InteropServices; | |
| var alloc = new Allocation(); | |
| Console.WriteLine("Hey, this tool will allocate how much memory you'll type in and reallocate "); | |
| try { | |
| Task.Run(async () => { | |
| while (true) { | |
| await Task.Delay(60000).ConfigureAwait(false); | |
| if (!alloc.Buffer.HasValue) | |
| continue; | |
| unsafe { | |
| var longptr = (ulong*) alloc.Buffer.Value; | |
| var byteptr = (byte*) alloc.Buffer.Value; | |
| ulong i, idx; | |
| for (i = 0, idx = 0; i < alloc.BufferSize - sizeof(ulong) * 2; i += sizeof(ulong), idx++) { | |
| longptr[idx] = i; | |
| } | |
| for (; i < alloc.BufferSize; i++) { | |
| byteptr[i] = (byte) (i % ((long) byte.MaxValue)); | |
| } | |
| } | |
| } | |
| }); | |
| while (true) { | |
| unsafe { | |
| Console.WriteLine("Enter memory size (bytes) to occupy: 1kb, 5mb, 12gb"); | |
| Console.Write("> "); | |
| var mem = GetNumberUnFormatted(Console.ReadLine()?.Trim()); | |
| if (!mem.HasValue) | |
| continue; | |
| Console.WriteLine(mem.Value); | |
| alloc.BufferSize = (ulong) mem.Value; | |
| if (alloc.Buffer.HasValue) { | |
| Marshal.FreeHGlobal(alloc.Buffer.Value); | |
| alloc.Buffer = null; | |
| } | |
| if (alloc.BufferSize == 0) | |
| continue; | |
| alloc.Buffer = Marshal.AllocHGlobal(new IntPtr((long) alloc.BufferSize)); | |
| var longptr = (ulong*) alloc.Buffer.Value; | |
| var byteptr = (byte*) alloc.Buffer.Value; | |
| ulong i, idx; | |
| for (i = 0, idx = 0; i < alloc.BufferSize - sizeof(ulong) * 2; i += sizeof(ulong), idx++) { | |
| longptr[idx] = i; | |
| } | |
| for (; i < alloc.BufferSize; i++) { | |
| byteptr[i] = (byte) (i % ((ulong) byte.MaxValue)); | |
| } | |
| } | |
| } | |
| } finally { | |
| if (alloc.Buffer.HasValue) { | |
| Marshal.FreeHGlobal(alloc.Buffer.Value); | |
| alloc.Buffer = null; | |
| } | |
| } | |
| static decimal? GetNumberUnFormatted(ReadOnlySpan<char> numberStr) { | |
| if (numberStr.IsEmpty) { | |
| return | |
| null; | |
| } | |
| var mod = new string(numberStr.ToArray().Reverse().TakeWhile(char.IsLetter).Reverse().ToArray()).ToLower(); | |
| uint multiplier = mod switch { | |
| "kb" => 1024, | |
| "mb" => 1048576, | |
| "gb" => 1073741824, | |
| "" => 1, | |
| _ => 0, | |
| }; | |
| if (multiplier == 0) { | |
| Console.WriteLine("Bad modifier value: " + mod); | |
| return null; | |
| } | |
| decimal number = multiplier * decimal.Parse(multiplier == 1 ? numberStr : numberStr[..^mod.Length]); | |
| return number; | |
| } | |
| class Allocation { | |
| public ulong BufferSize = 0; | |
| public IntPtr? Buffer; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment