Skip to content

Instantly share code, notes, and snippets.

@FrostBird347
Last active March 29, 2024 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrostBird347/90dd48c54fdde69a00ab641b1ee823f2 to your computer and use it in GitHub Desktop.
Save FrostBird347/90dd48c54fdde69a00ab641b1ee823f2 to your computer and use it in GitHub Desktop.
A simple BetterDiscord plugin that adds a button to flush discord's cache.
/**
* @name PurgeMemory
* @authorLink https://github.com/FrostBird347
* @source https://gist.github.com/FrostBird347/90dd48c54fdde69a00ab641b1ee823f2#gistcomment-4388425
* @updateUrl https://gist.githubusercontent.com/FrostBird347/90dd48c54fdde69a00ab641b1ee823f2/raw/PurgeMemory.plugin.js
*/
module.exports = class PurgeMemory {
getName () {return "PurgeMemory";}
getDescription () {return "Attempts to flush discord's memory. Doesn't always work well enough, but I have observed best results when used multiple times.";}
getVersion() {return "1.1.4";}
getAuthor () {return "FrostBird347";}
load() {}
stop() {
clearInterval(this.buttonCheckID);
clearInterval(this.buttonUpdateID);
try {
this.PurgeMemoryButton.remove();
} catch {}
}
start() {
//Simple update notifier
//I have been unable to figure out how to obtain the plugin's version
let CurrentVersion = "1.1.4"
window.fetch("https://gist.githubusercontent.com/FrostBird347/90dd48c54fdde69a00ab641b1ee823f2/raw/PurgeMemory.plugin.js").then(function(response) {
response.text().then(function(text) {
let Lines = text.split("\t").join("").split("\n")
let RemoteVersion = ""
for (var l = 0; l < Lines.length; l++) {
if (RemoteVersion == "" && Lines[l].split(" ").join("").split("\"")[0] == "getVersion(){return") {
RemoteVersion = Lines[l].split(" ").join("").split("\"")[1]
}
}
if (RemoteVersion != CurrentVersion) {
window.BdApi.UI.showToast("PurgeMemory is outdated!\nCurrent: " + CurrentVersion + "\nLatest: " + RemoteVersion, {type: "info", icon:true, timeout: 7500})
}
})
})
this.ElementClasses = {
"Toolbar": "toolbar__62fb5",
"IconWrapper": "iconWrapper_de6cd1",
"Clickable": "clickable_ce0925",
"Icon": "icon_ae0b42"
}
this.buttonCheckID = setInterval(this.buttonCheck, 5000, this);
this.buttonUpdateID = setInterval(this.buttonUpdate, 2500, this);
}
buttonCheck(Plugin) {
let ToolbarElement = document.getElementsByClassName(Plugin.ElementClasses.Toolbar)[0];
if (ToolbarElement != undefined && ToolbarElement.getElementsByClassName("PurgeMemoryButton").length == 0) {
Plugin.PurgeMemoryButton = document.createElement("div");
Plugin.PurgeMemoryButton.className = "PurgeMemoryButton " + Plugin.ElementClasses.IconWrapper + " " + Plugin.ElementClasses.Clickable;
Plugin.PurgeMemoryButton.attributes.role = "Button";
Plugin.PurgeMemoryButton.ariaLabel = "Inbox";
Plugin.PurgeMemoryButton.ariaExpanded = false;
Plugin.PurgeMemoryButton.tabIndex = 0;
Plugin.PurgeMemoryButton.innerHTML = '<svg x="0" y="0" class="' + Plugin.ElementClasses.Icon + '" aria-hidden="true" role="img" width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M15 3.999V2H9V3.999H3V5.999H21V3.999H15Z" fill="currentColor"></path><path d="M5 6.99902V18.999C5 20.101 5.897 20.999 7 20.999H17C18.103 20.999 19 20.101 19 18.999V6.99902H5ZM11 17H9V11H11V17ZM15 17H13V11H15V17Z" fill="currentColor"></path></svg>';
ToolbarElement.prepend(Plugin.PurgeMemoryButton);
Plugin.PurgeMemoryTooltip = BdApi.UI.createTooltip(Plugin.PurgeMemoryButton, "Purge Memory", { style: "primary" });
Plugin.PurgeMemoryButton.addEventListener('click', function(event) {
Plugin.PurgeMemoryButton.children[0].style.color = "var(--brand-experiment)";
Plugin.PurgeMemoryButton.style.pointerEvents = "none";
for (let i = 0; i < 8; i++) {
setTimeout(function() {
DiscordNative.processUtils.purgeMemory();
console.log("PurgeMemory triggered");
}, i * 625)
}
setTimeout(function() {
Plugin.PurgeMemoryButton.children[0].style.color = "";
Plugin.PurgeMemoryButton.style.pointerEvents = "";
}, 5000)
}, false)
}
}
buttonUpdate(Plugin) {
if (Plugin.PurgeMemoryButton != undefined && Plugin.PurgeMemoryTooltip != undefined) {
require('process').getProcessMemoryInfo().then(function(Memory) {
Plugin.PurgeMemoryTooltip.label = "Purge Memory (" + Math.round(Memory.private / 1000) + "MB)"
})
}
}
}
@FrostBird347
Copy link
Author

FrostBird347 commented Dec 2, 2022

Source ↑

Update Log

1.1.4

  • Discord changed the class names of everything
  • Replaced hardcoded colours with variables, thus making the plugin compatible with themes

1.1.3

  • Updated deprecated BdApi functions

1.1.2

  • Discord changed the class names of everything

1.1.1

  • Fixed a visual issue with the button after being pressed

1.1.0

  • Button now changes colour when pressed
  • The purge function is run twice as many times as before, over a period of 5 seconds instead of all at once
  • Update memory text more often as it seems to be less expensive and faster I expected it to be

1.0.0

  • Created plugin

@Pickymarker
Copy link

the PurgeMemory button does nothing when i click on it

@FrostBird347
Copy link
Author

FrostBird347 commented Dec 2, 2022

the PurgeMemory button does nothing when i click on it

When clicked the button runs the function DiscordNative.processUtils.purgeMemory() a few times and prints a log in the console.
I have updated the plugin to give a visual indicator that the users input has been received.

The purge function itself does not immediately cause the ram usage to go down and seems to work better when pressed multiple times.
In addition to this, the tooltip text is only updated once every 2.5 seconds and must be hovered over again before the updated text will be displayed.

@Pickymarker
Copy link

you should use https://openasar.dev/ and https://github.com/clachoverclan/DiscordRamLimit to have discord use the least amount of resources

@FrostBird347
Copy link
Author

you should use https://openasar.dev/ and https://github.com/clachoverclan/DiscordRamLimit to have discord use the least amount of resources

Unfortunately, the ram limit program is not an option for users on operating systems other than windows and OpenAsar, while improving performance doesn't fix whatever memory leak my client has.

For anybody else in the future reading this, I strongly recommend auditing/verifying the validity of both programs and coming to your own conclusion on whether continued use of said programs (especially OpenAsar, which auto updates) are a worthwhile risk before using them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment