Skip to content

Commit

Permalink
Smarter garbage collection.
Browse files Browse the repository at this point in the history
Move garbage collector calls to better spots, and run only when total used memory crosses a threshold (150MB) for non-critical areas.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
  • Loading branch information
itdelatrisu committed Feb 1, 2017
1 parent c95fab3 commit baf35e3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/itdelatrisu/opsu/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public class Utils {
Arrays.sort(illegalChars);
}

/** Minimum memory used by the JVM (in bytes) before running "optional" garbage collection. */
private static final long GC_MEMORY_THRESHOLD = 150 * 1_000_000L; // 150MB

/** Baseline memory used by the JVM (in bytes). */
private static long baselineMemoryUsed = 0;

// game-related variables
private static Input input;

Expand Down Expand Up @@ -689,4 +695,23 @@ public static void setSSLCertValidation(boolean enabled) {
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {}
}

/**
* Runs the garbage collector.
* @param force if false, garbage collection will only run if current memory
* usage is above a threshold
*/
public static final void gc(boolean force) {
if (!force && getUsedMemory() - baselineMemoryUsed < GC_MEMORY_THRESHOLD)
return;

System.gc();
baselineMemoryUsed = getUsedMemory();
}

/** Returns the amount memory used by the JVM (in bytes). */
public static long getUsedMemory() {
Runtime r = Runtime.getRuntime();
return r.totalMemory() - r.freeMemory();
}
}
3 changes: 2 additions & 1 deletion src/itdelatrisu/opsu/audio/MusicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package itdelatrisu.opsu.audio;

import itdelatrisu.opsu.ErrorHandler;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.beatmap.Beatmap;
import itdelatrisu.opsu.beatmap.BeatmapParser;
import itdelatrisu.opsu.beatmap.TimingPoint;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static void play(final Beatmap beatmap, final boolean loop, final boolean
}

reset();
System.gc();
Utils.gc(false);

switch (BeatmapParser.getExtension(beatmap.audioFilename.getName())) {
case "ogg":
Expand Down
2 changes: 1 addition & 1 deletion src/itdelatrisu/opsu/beatmap/BeatmapSetList.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ else if (ePrev != null && ePrev.index == expandedIndex)
File audioFile = MusicController.getBeatmap().audioFilename;
if (audioFile != null && audioFile.equals(beatmap.audioFilename)) {
MusicController.reset();
System.gc(); // TODO: why can't files be deleted without calling this?
Utils.gc(true); // TODO: why can't files be deleted without calling this?
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/itdelatrisu/opsu/states/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,8 @@ public void loadBeatmap(Beatmap beatmap) {
BeatmapDB.load(beatmap, BeatmapDB.LOAD_ARRAY);
BeatmapParser.parseHitObjects(beatmap);
HitSound.setDefaultSampleSet(beatmap.sampleSet);

Utils.gc(true);
}

/**
Expand Down Expand Up @@ -1741,8 +1743,6 @@ public void resetGameData() {
video = null;
}
videoSeekTime = 0;

System.gc();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/itdelatrisu/opsu/states/Splash.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public void run() {
if (newSkin)
SoundController.init();

Utils.gc(true);

finished = true;
thread = null;
}
Expand Down Expand Up @@ -169,6 +171,8 @@ public void run() {
// load sounds
SoundController.init();

Utils.gc(true);

finished = true;
thread = null;
}
Expand Down

0 comments on commit baf35e3

Please sign in to comment.