Skip to content

Commit b33343f

Browse files
authored
Switch to sysctl to fetch memory size
1 parent d1f20e6 commit b33343f

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

src/nativeStub.m

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#import <stdlib.h>
33
#import <string.h>
44
#import <AppKit/AppKit.h>
5+
#include <sys/sysctl.h>
56

67
int main(int argc, char** argv) {
78
// For future improvement we can make these localized strings actually have the translations
@@ -376,22 +377,29 @@ int main(int argc, char** argv) {
376377

377378
// If no max memory setting is present, calculate and add it
378379
if (!hasMaxMemorySetting) {
379-
unsigned long long totalRAM = [[NSProcessInfo processInfo] physicalMemory];
380-
unsigned long long maxMemory = (totalRAM * 75) / 100; // 75% of total RAM
381-
double maxMemoryGB = (double)maxMemory / (1024 * 1024 * 1024); // Convert to GB
382-
383-
NSString *maxMemoryOption;
384-
if (maxMemoryGB >= 1) {
385-
// Round down to the nearest whole GB
386-
unsigned long long roundedGB = (unsigned long long)maxMemoryGB;
387-
maxMemoryOption = [NSString stringWithFormat:@"-Xmx%lluG", roundedGB];
388-
} else {
389-
// Specify in MB if less than 1 GB
390-
unsigned long long maxMemoryMB = maxMemory / (1024 * 1024); // Convert to MB
391-
maxMemoryOption = [NSString stringWithFormat:@"-Xmx%lluM", maxMemoryMB];
392-
}
393-
394-
[jvmDefaultOptions addObject:maxMemoryOption];
380+
// Get total system memory in bytes using sysctl
381+
unsigned long long totalRAM = getTotalRAM();
382+
383+
if (totalRAM == 0) {
384+
NSLog(@"Failed to retrieve total RAM. Skipping max memory calculation.");
385+
} else {
386+
// Calculate 75% of the total RAM
387+
unsigned long long maxMemory = (totalRAM * 75) / 100;
388+
389+
NSString *maxMemoryOption;
390+
if (maxMemory >= (1024 * 1024 * 1024)) {
391+
// If memory is at least 1 GB, round down to the nearest GB
392+
unsigned long long maxMemoryGB = maxMemory / (1024 * 1024 * 1024); // Convert to GB
393+
maxMemoryOption = [NSString stringWithFormat:@"-Xmx%lluG", maxMemoryGB];
394+
} else {
395+
// If memory is less than 1 GB, specify in MB
396+
unsigned long long maxMemoryMB = maxMemory / (1024 * 1024); // Convert to MB
397+
maxMemoryOption = [NSString stringWithFormat:@"-Xmx%lluM", maxMemoryMB];
398+
}
399+
400+
// Add the calculated max memory option to the JVM options
401+
[jvmDefaultOptions addObject:maxMemoryOption];
402+
}
395403
}
396404
}
397405

@@ -551,3 +559,15 @@ BOOL versionMeetsMaxConstraint(NSString *version, NSString *constraint) {
551559
return !exceeds && [constraintParts count] == [versionParts count];
552560
}
553561
}
562+
563+
unsigned long long getTotalRAM() {
564+
int64_t physicalMemory = 0;
565+
size_t length = sizeof(physicalMemory);
566+
567+
if (sysctlbyname("hw.memsize", &physicalMemory, &length, NULL, 0) == 0) {
568+
return (unsigned long long)physicalMemory;
569+
} else {
570+
perror("sysctlbyname failed");
571+
return 0; // Return 0 if the system call fails
572+
}
573+
}

0 commit comments

Comments
 (0)