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

fix add message tip when OutOfMemory occurred for issue #2323 #2331

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ void ensureCapacity(int minCapacity) {
newCapacity = minCapacity;
}
if (newCapacity > maxArraySize) {
throw new OutOfMemoryError();
throw new OutOfMemoryError("try enabling LargeObject feature instead");
}

// minCapacity is usually close to size, so this is a win:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ final void ensureCapacity(int minCapacity) {
newCapacity = minCapacity;
}
if (newCapacity - maxArraySize > 0) {
throw new OutOfMemoryError();
throw new OutOfMemoryError("try enabling LargeObject feature instead");
}

// minCapacity is usually close to size, so this is a win:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ final void ensureCapacity(int minCapacity) {
newCapacity = minCapacity;
}
if (newCapacity - maxArraySize > 0) {
throw new OutOfMemoryError();
throw new OutOfMemoryError("try enabling LargeObject feature instead");
}

// minCapacity is usually close to size, so this is a win:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.fastjson2.issues_2300;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

public class Issue2323 {
String errMsg = "try enabling LargeObject feature instead";

@Test
public void test() throws Exception {
int size = 50;

List<JSONObject> dataList2 = Lists.newArrayList();
for (int rowIndex = 1; rowIndex <= 50000; rowIndex++) {
JSONObject data = new JSONObject();
for (int i = 0; i < size; i++) {
data.put("test" + i, "testdata");
}
dataList2.add(data);
}

Map<String, String> params = Maps.newHashMap();
params.put("dataListStr", JSON.toJSONString(dataList2));

try {
JSONWriter.ofUTF16().write(params);
} catch (OutOfMemoryError error) {
Assertions.assertEquals(errMsg, error.getMessage());
}

try {
JSONWriter.ofUTF8().write(params);
} catch (OutOfMemoryError error) {
Assertions.assertEquals(errMsg, error.getMessage());
}

try {
JSONWriter.ofJSONB().write(params);
} catch (OutOfMemoryError error) {
Assertions.assertEquals(errMsg, error.getMessage());
}
}
}
Loading