Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set limits for maximum memory use. #4147

Merged
merged 8 commits into from
Aug 19, 2021
2 changes: 1 addition & 1 deletion engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ dependencies {
api group: 'org.codehaus.plexus', name: 'plexus-utils', version: '1.5.6'

// Java magic
implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '4.2.2'
implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '4.5.2'
keturn marked this conversation as resolved.
Show resolved Hide resolved
implementation group: 'org.reflections', name: 'reflections', version: '0.9.10'
implementation group: 'org.javassist', name: 'javassist', version: '3.20.0-GA'
implementation group: 'com.esotericsoftware', name: 'reflectasm', version: '1.11.1'
Expand Down
18 changes: 3 additions & 15 deletions facades/PC/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

// The PC facade is responsible for the primary distribution - a plain Java application runnable on PCs

Expand Down Expand Up @@ -97,6 +84,7 @@ group = 'org.terasology.facades'
dependencies {
implementation project(':engine')
implementation group: 'org.reflections', name: 'reflections', version: '0.9.10'
implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '4.5.2'

// TODO: Consider whether we can move the CR dependency back here from the engine, where it is referenced from the main menu
implementation group: 'org.terasology.crashreporter', name: 'cr-terasology', version: '4.1.0'
Expand Down
45 changes: 30 additions & 15 deletions facades/PC/src/main/java/org/terasology/engine/Terasology.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
/*
* Copyright 2020 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.sun.jna.Platform;
import com.sun.jna.platform.unix.LibC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.config.Config;
Expand Down Expand Up @@ -59,8 +48,10 @@
import java.awt.Rectangle;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;

import static com.google.common.base.Verify.verifyNotNull;
Expand Down Expand Up @@ -131,6 +122,8 @@ public static void main(String[] args) {

handlePrintUsageRequest(args);
handleLaunchArguments(args);
setMemoryLimit(1024 * 8); // TODO: make arg
adjustOutOfMemoryScore(200); // TODO: make arg

SplashScreen splashScreen = splashEnabled ? configureSplashScreen() : SplashScreenBuilder.createStub();

Expand Down Expand Up @@ -477,4 +470,26 @@ private static int getLastNumber(String str) {
return Integer.parseInt(str.substring(positionOfLastDigit));
}

private static void setMemoryLimit(long maxMegabytes) {
// Memory-limiting techniques are highly platform-specific.
if (Platform.isLinux()) {
final LibC.Rlimit dataLimit = new LibC.Rlimit();
long maxBytes = maxMegabytes * 1024 * 1024;
dataLimit.rlim_cur = maxBytes;
dataLimit.rlim_max = maxBytes;
// Under Linux ≥ 4.7, we can limit the maximum size of the process's data segment, which includes its
// heap. Note we cannot directly limit its resident set size, see setrlimit(3).
LibC.INSTANCE.setrlimit(LibC.RLIMIT_DATA, dataLimit);
}
}

private static void adjustOutOfMemoryScore(int adjustment) {
Path procFile = Paths.get("/proc", "self", "oom_score_adj");
try {
Files.write(procFile, String.valueOf(adjustment).getBytes(),
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
keturn marked this conversation as resolved.
Show resolved Hide resolved
}
}
}