From ce5e3bfeb7b5c80b99fe565d2c581f9bf8e503e9 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 16 Dec 2021 18:39:26 +0300 Subject: [PATCH] C/Wasm: fix issue in GC --- core/src/main/java/org/teavm/runtime/GC.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/teavm/runtime/GC.java b/core/src/main/java/org/teavm/runtime/GC.java index 34666bfab..c5a58d542 100644 --- a/core/src/main/java/org/teavm/runtime/GC.java +++ b/core/src/main/java/org/teavm/runtime/GC.java @@ -115,12 +115,21 @@ public final class GC { return; } collectGarbageImpl(size); - if (currentChunk.size != size && currentChunk.size <= size + MIN_CHUNK_SIZE && !getNextChunkIfPossible(size)) { - ExceptionHandling.printStack(); - outOfMemory(); + if (!hasAvailableMemory(size)) { + collectGarbageFullImpl(size); + if (!hasAvailableMemory(size)) { + ExceptionHandling.printStack(); + outOfMemory(); + } } } + private static boolean hasAvailableMemory(int size) { + return currentChunk.size == size + || currentChunk.size > size + MIN_CHUNK_SIZE + || getNextChunkIfPossible(size); + } + private static boolean getNextChunkIfPossible(int size) { while (true) { if (currentChunk.toAddress().isLessThan(currentChunkLimit)) {