Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions source/funkin/backend/system/framerate/MemoryCounter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class MemoryCounter extends Sprite {
addChild(label);
}
memoryPeakText.alpha = 0.5;
#if !(cpp && (windows || mac || linux))
memoryPeakText.visible = false;
#end
}

public function reload() {}
Expand All @@ -36,6 +39,21 @@ class MemoryCounter extends Sprite {
if (alpha <= 0.05) return;
super.__enterFrame(t);

#if (cpp && (windows || mac || linux))
final gcMem = MemoryUtil.currentMemUsage();
final osMem = MemoryUtil.currentProcessMemUsage();

if (gcMem == memory && osMem == memoryPeak) {
updateLabelPosition();
return;
}

memory = gcMem;
memoryPeak = osMem;

memoryText.text = CoolUtil.getSizeString(gcMem);
memoryPeakText.text = ' / ${CoolUtil.getSizeString(osMem)}';
#else
final mem = MemoryUtil.currentMemUsage();

if (mem == memory) {
Expand All @@ -44,9 +62,8 @@ class MemoryCounter extends Sprite {
}

memory = mem;
if (memoryPeak < memory) memoryPeak = memory;
memoryText.text = CoolUtil.getSizeString(memory);
memoryPeakText.text = ' / ${CoolUtil.getSizeString(memoryPeak)}';
memoryText.text = CoolUtil.getSizeString(mem);
#end

updateLabelPosition();
}
Expand Down
17 changes: 17 additions & 0 deletions source/funkin/backend/utils/MemoryUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@ final class MemoryUtil {
#end
}

/**
* Gets the current process memory usage (Working Set / RSS) from the OS.
* This is much more granular than Haxe GC memory and updates more frequently.
* Returns the value in bytes, matching what you see in Task Manager / Activity Monitor.
*/
public static inline function currentProcessMemUsage():Float {
#if (cpp && windows)
return funkin.backend.utils.native.Windows.getCurrentProcessMemory();
#elseif (cpp && mac)
return funkin.backend.utils.native.Mac.getCurrentProcessMemory();
#elseif (cpp && linux)
return funkin.backend.utils.native.Linux.getCurrentProcessMemory();
#else
return currentMemUsage();
#end
}


/**
* Gets the memory type of the system.
Expand Down
20 changes: 20 additions & 0 deletions source/funkin/backend/utils/native/Linux.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,25 @@ final class Linux {
{
return 0;
}

@:functionCode('
FILE *status = fopen("/proc/self/status", "r");
if (status == NULL) return 0.0;

char line[256];
while (fgets(line, sizeof(line), status)) {
long rss;
if (sscanf(line, "VmRSS: %ld kB", &rss) == 1) {
fclose(status);
return (double)(rss * 1024); // kB -> bytes
}
}
fclose(status);
return 0.0;
')
public static function getCurrentProcessMemory():Float
{
return 0;
}
}
#end
14 changes: 14 additions & 0 deletions source/funkin/backend/utils/native/Mac.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import funkin.backend.utils.NativeAPI.CodeCursor;
import openfl.ui.Mouse;

@:headerInclude('sys/sysctl.h')
@:headerInclude('mach/mach.h')
@:dox(hide)
final class Mac
{
Expand All @@ -23,6 +24,19 @@ final class Mac
return 0;
}

@:functionCode('
task_basic_info_data_t info;
mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count) == KERN_SUCCESS) {
return (double)info.resident_size;
}
return 0.0;
')
public static function getCurrentProcessMemory():Float
{
return 0;
}

public static function setMouseCursorIcon(icon:CodeCursor):Void
{
final valid:Bool = external.ExternalMac.setCursorIcon(icon.toInt(), null, 0, 0);
Expand Down
13 changes: 13 additions & 0 deletions source/funkin/backend/utils/native/Windows.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import funkin.backend.utils.NativeAPI.MessageBoxIcon;
#include <wingdi.h>
#include <shellapi.h>
#include <uxtheme.h>
#include <psapi.h>

#define SAFE_RELEASE(punk) \\
if ((punk) != NULL) \\
Expand Down Expand Up @@ -267,5 +268,17 @@ final class Windows {
{
return 0;
}

@:functionCode("
PROCESS_MEMORY_COUNTERS_EX pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc))) {
return (double)pmc.WorkingSetSize;
}
return 0.0;
")
public static function getCurrentProcessMemory():Float
{
return 0;
}
}
#end