|
2 | 2 | #import <stdlib.h> |
3 | 3 | #import <string.h> |
4 | 4 | #import <AppKit/AppKit.h> |
| 5 | +#include <sys/sysctl.h> |
5 | 6 |
|
6 | 7 | int main(int argc, char** argv) { |
7 | 8 | // For future improvement we can make these localized strings actually have the translations |
@@ -376,22 +377,29 @@ int main(int argc, char** argv) { |
376 | 377 |
|
377 | 378 | // If no max memory setting is present, calculate and add it |
378 | 379 | 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 | + } |
395 | 403 | } |
396 | 404 | } |
397 | 405 |
|
@@ -551,3 +559,15 @@ BOOL versionMeetsMaxConstraint(NSString *version, NSString *constraint) { |
551 | 559 | return !exceeds && [constraintParts count] == [versionParts count]; |
552 | 560 | } |
553 | 561 | } |
| 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