Skip to content

Commit

Permalink
Merge pull request #2040 from lf-lang/test-fix-free-memory
Browse files Browse the repository at this point in the history
Fixed trimming of the recorded test output
  • Loading branch information
erlingrj authored Oct 5, 2023
2 parents 3889b1a + 475306e commit 0042432
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions core/src/testFixtures/java/org/lflang/tests/LFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,16 @@ private Thread recordStream(StringBuffer builder, InputStream inputStream) {
int len;
char[] buf = new char[1024];
while ((len = reader.read(buf)) > 0) {
if (Runtime.getRuntime().freeMemory() < Runtime.getRuntime().totalMemory() / 2) {
builder.append(buf, 0, len);
// If the buffer gets too large, then we delete the first half of the buffer
// and trim it down in size. It is important to decide what "too large" means.
// Here we take 1/4 of the total memory available to the Java runtime as a rule of
// thumb.
if (builder.length() > Runtime.getRuntime().totalMemory() / 4) {
builder.delete(0, builder.length() / 2);
builder.insert(0, "[earlier messages were removed to free up memory]%n");
builder.insert(0, "[earlier messages were removed to free up memory]\n");
builder.trimToSize();
}
builder.append(buf, 0, len);
}
} catch (IOException e) {
throw new RuntimeIOException(e);
Expand Down

0 comments on commit 0042432

Please sign in to comment.