- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 Java 程序,正在读取大小约为 40GB 的文件,并动态转换数据并流式传输到 DataOutputstream。
下面是我的代码
JSONObject jsonObj= new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject properties = new JSONObject();
while((strLine = in.readLine()) != null)
{
if (jsonArray.length()%100 == 0) {
printJsonData(jsonArray);
// sendJsonData(jsonArray, output);
jsonArray = new JSONArray();
}
if (strLine.trim().contains("data")) {
jsonObj.put("properties",properties);
jsonArray.put(jsonObj);
if (strLine.trim().contains("data1")) {
String secondPart = strLine.split(":",2)[1];
properties.put("data1", secondPart);
continue;
}
if (strLine.trim().contains("id")) {
String secondPart = strLine.split(":",2)[1];
jsonObj.put("id", secondPart );
continue;
}
}
我有两种方法,一种是打印 JSON 数据,另一种是通过 https 发送 Json 数据。
private void printJsonData(JSONArray jsonArray) {
int count = jsonArray.length(); // get totalCount of all jsonObjects
for(int i=0 ; i< count; i++){ // iterate through jsonArray
JSONObject jsonObject = jsonArray.getJSONObject(i); // get jsonObject @ i position
System.out.println("jsonObject " + i + ": " + jsonObject);
}
}
private void sendJsonData(JSONObject jsonObj,DataOutputStream output) throws IOException {
int count = jsonArray.length();
for(int i=0 ; i< count; i++) { // iterate through jsonArray
JSONObject jsonObject = jsonArray.getJSONObject(i); // get jsonObject @ i position
System.out.println("Sending Data-->" + jsonObject.toString());
output.writeBytes(jsonObject.toString());
}
}
每当我调用打印方法时,一切都正常。但是当我调用 sendJsonData 方法时。我遇到内存不足错误。想知道如何修复它吗?
最佳答案
由于您没有以任何方式操作 json,所以我会将其视为文本文件并以这种方式发送。找到大 JsonObject 的可能性很大,因此您可能会付出太多努力。
除非有巨大的对象,否则 40GB 的数组是非常大的数组。
** 编辑 **
或者您可以将各个 json 对象视为"file"并单独推送这些字符串
关于java - 将 Json 数组写入输出流时出现 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662098/
我正在尝试将用户提供的经纬度值与数据库中的经纬度值进行比较。如果它们在彼此半径 15 公里内,则应更改 TextView 。但我面临以下错误, 我的数据库包含值 source lat = 19.218
我在我的应用程序中使用改造来下载一些媒体文件,如视频、mp3、jpg、pdf 等。当我想下载一个 55MB 的 mp4 格式的大文件时,这是一个问题。当我想下载这个文件时,我收到这样的错误: OutO
所以我正在创建一个 Android 应用程序,这段代码引发了 "Caused by: java.lang.OutOfMemoryError: OutOfMemoryError thrown while
直到昨天,我的应用程序运行良好,但我所做的是,由于某些原因,我不得不在 Android Studio 中打开具有不同工作空间的同一个应用程序。从那时起,当我尝试运行该应用程序时,我遇到了以下异常,所以
我正在尝试构建一个应用程序,其中客户端将其屏幕发送到服务器,客户端仅在上次发送屏幕和最新捕获的屏幕之间存在差异时才发送其屏幕(以便该程序在网络)。服务器使用 JFrame 和 JLabel 来显示图像
我正在尝试使用内存映射模式在 cupy 中加载一些较大的 .npy 文件,但我不断遇到 OutOfMemoryError 。 我认为,由于它是在内存映射模式下打开的,因此此操作不应该占用太多内存,因为
我正在尝试对基于 ant 的(Netbeans RCP)项目进行分级并找到奇怪的分级行为。 我用探查器做了一些观察,得到了下一个结果。 环境配置 Gradle 1.9 Build time: 20
我有一个应用程序可以进行网络调用并检索 XML 数据。如果没有太多数据,下面的代码可以正常工作。 public class WebClient { private static final S
在我的应用程序中,我每 3 分钟刷新一次数据。如果应用程序可以工作几个小时,我会遇到这样的错误: java.lang.OutOfMemoryError at org.apache.http.util.
我在我的一个应用程序中偶尔收到 OutOfMemoryError: (Heap Size=49187KB, Allocated=41957KB)。我该怎么做才能诊断? 01-09 10:32:02
对于学校项目,我必须编写不同类型的算法。问题是,我得到了一个工作算法。但是我必须多次运行它,一段时间后它给了我以下错误: Exception in thread "main" java.lang.Ou
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Recursive function causing a stack overflow 完成示例惰性序列 here
我收到 java.lang.OutOfMemoryError 错误,即使我还有足够的空闲 RAM。我进行的内存转储在 200MB 到 1GB 之间,而我的服务器有 24GB 的 RAM。我设置了 -X
我不明白为什么这段代码没有OutOfMemoryError public static void main(String[] args) { Object[] ref = new Object
我正在使用这个语句 //some code int a[][]=new int[5000000][5000000]; //some code 并使用命令运行它 java -mx512m Test 它给
今天我在玩OOM错误,我发现了一些我自己无法解释的东西。 我尝试分配一个比堆大的数组,期望 “请求的阵列大小超出 VM 限制”错误,但我得到一个“ Java 堆空间 ”错误。 根据JDK 11 doc
我有一个显示图像的简单页面。来源是 URL var img = new Image (); var source = new UriImageSource { Uri =
我有一个 Java Spring Boot 应用程序。它是一个非常大的应用程序,具有许多服务,并且可以执行大量任务。我尝试实现的新任务之一是从 Oracle DB 读取一些数据并通过 REST 将其发
我正在尝试使用流读取一个非常大的文件,因此我需要并行流而不是每行迭代...我正在尝试如下: String cont = new String(Files.readAllBytes(Paths.get(
假设我们的最大内存为 256M,为什么这段代码可以工作: public static void main(String... args) { for (int i = 0; i < 2; i++)
我是一名优秀的程序员,十分优秀!