Repository: Cog-Creators/Red-DiscordBot

Issue:
[Audio] Adjust max heapsize for 32 bit devices
### What Red version are you using?

3.5.1

### What were you trying to do?

Help Raspberry Pi 3b+ users in support with their bot managing and launching the Lavalink.jar.

### What did you expect to happen?

I expected the bot to launch, download the jar if needed, start the jar, and then have Audio connect to it.

### What actually happened?

The Lavalink.jar seemed to lock up before fully completing the startup or in some cases, lock up when a connection from Red to Lavalink was initiated. Logs were incomplete where I expected to see Red connection lines or standard Lavalink log lines.

### How can we reproduce this error?

1. Have a 32 bit OS/device with a smaller amount of RAM, like Raspbian 11 on a Pi 3b+ (with 1GB RAM, which was the case for both Red users that reported this in support)
2. Install Red
3. Start the bot and load audio
4. The jar will seemingly not be able to get to a point where Red can connect to it. Running the jar as an externally managed process without an Xmx flag, or where Xmx is equal to 512mb or less, will let it launch and have Red connect (have not tested further yet, this comment is based on what I saw in Red support)

The proposed fix is:

<https://github.com/Cog-Creators/Red-DiscordBot/blob/4daf81aa5b8d9140ae2295548d22566b07f64a71/redbot/cogs/audio/utils.py#L30>

`min(4 * 1024**3, psutil.virtual_memory().total)` may work for this

https://github.com/Cog-Creators/Red-DiscordBot/blob/4daf81aa5b8d9140ae2295548d22566b07f64a71/redbot/cogs/audio/utils.py#L39

```
    _min_max = 512 * 1024**2
    max_ram_allowed = max(max_allocation * 0.5, _min_max)
```

I am not PRing this change myself at this point as maybe someone would like to work on this before I have time to do so. I have a Pi 3b+ in the configuration needed to reproduce this, so if there is further testing needed on a PR please let me know and I can help.

### Anything else?

See https://discord.com/channels/133049272517001216/387398816317440000/1104968352625078313 in Red's support for proof that it reserves 4GB on the current version of Red

See https://discord.com/channels/133049272517001216/387398816317440000/1105198290452086814 for a second user where setting the Xmx smaller helped


Discussion:

Resolving patch:
diff --git a/redbot/cogs/audio/utils.py b/redbot/cogs/audio/utils.py
--- a/redbot/cogs/audio/utils.py
+++ b/redbot/cogs/audio/utils.py
@@ -27,7 +27,7 @@ def get_max_allocation_size(exec) -> Tuple[int, bool]:
         max_heap_allowed = psutil.virtual_memory().total
         thinks_is_64_bit = True
     else:
-        max_heap_allowed = 4 * 1024**3
+        max_heap_allowed = min(4 * 1024**3, psutil.virtual_memory().total)
         thinks_is_64_bit = False
     return max_heap_allowed, thinks_is_64_bit
 
@@ -36,7 +36,7 @@ def get_jar_ram_defaults() -> Tuple[str, str]:
     min_ram = 64 * 1024**2
     # We don't know the java executable at this stage - not worth the extra work required here
     max_allocation, is_64bit = get_max_allocation_size(sys.executable)
-    max_ram_allowed = max_allocation * 0.5 if is_64bit else max_allocation
+    max_ram_allowed = min(max_allocation, psutil.virtual_memory().total * 0.5)
     max_ram = max(min_ram, max_ram_allowed)
     size_name = ("", "K", "M", "G", "T")
     i = int(math.floor(math.log(min_ram, 1024)))


Test patch: