Out of memory error in Kotlin when looping through a medium size data set
I am running the below loop in Kotlin and is throwing an out of memory error. I am running this for reading the rows in a csv file. Size of "records" is 6422.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = ""
records.forEach() {
val size = it.size()
for (i in 0 until it.size()-1) {
output = output + it.get(i) + ","
}
output.dropLast(1)
rows.add(output)
}
return rows
}
Below is the exception I get.
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at trivago.ti.tools.FileProcessor.readCSVFile(FileProcessor.kt:16)
at trivago.ti.tools.ComparatorMainKt.main(ComparatorMain.kt:25)
I have the same logic executing in Java but it works fine. Below is what I have in Java.
private static List<String> readCSVFile(String filePath) throws IOException {
Reader in = new FileReader(filePath);
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
List<String> rows = new ArrayList<>();
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < record.size(); i++)
builder.append(record.get(i) + ",");
builder.deleteCharAt(builder.length() - 1);
rows.add(builder.toString());
}
return rows;
}
Why is Kotlin having a problem with this? Am I doing something wrong with the loop? Any help would be much appreciated as I'm a newbie to Kotlin.
java memory kotlin jvm out-of-memory
add a comment |
I am running the below loop in Kotlin and is throwing an out of memory error. I am running this for reading the rows in a csv file. Size of "records" is 6422.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = ""
records.forEach() {
val size = it.size()
for (i in 0 until it.size()-1) {
output = output + it.get(i) + ","
}
output.dropLast(1)
rows.add(output)
}
return rows
}
Below is the exception I get.
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at trivago.ti.tools.FileProcessor.readCSVFile(FileProcessor.kt:16)
at trivago.ti.tools.ComparatorMainKt.main(ComparatorMain.kt:25)
I have the same logic executing in Java but it works fine. Below is what I have in Java.
private static List<String> readCSVFile(String filePath) throws IOException {
Reader in = new FileReader(filePath);
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
List<String> rows = new ArrayList<>();
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < record.size(); i++)
builder.append(record.get(i) + ",");
builder.deleteCharAt(builder.length() - 1);
rows.add(builder.toString());
}
return rows;
}
Why is Kotlin having a problem with this? Am I doing something wrong with the loop? Any help would be much appreciated as I'm a newbie to Kotlin.
java memory kotlin jvm out-of-memory
What size is the CSV you are reading? What's the -Xmx value of your JVM (if you defined any yourself) ?
– Sofo Gial
Nov 14 '18 at 14:33
Sorry I edited the question. its 6422
– mayooran
Nov 14 '18 at 14:34
In bytes? Also, did you define the -Xmx anywhere or is it default?
– Sofo Gial
Nov 14 '18 at 14:36
I guess the answers have solved the problem, so this is to solve another one: replacei in 0 until it.size()-1
withi in 0 until it.size()
ori in 0..it.size()-1
because the code as it is loses the last line.
– forpas
Nov 14 '18 at 14:58
add a comment |
I am running the below loop in Kotlin and is throwing an out of memory error. I am running this for reading the rows in a csv file. Size of "records" is 6422.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = ""
records.forEach() {
val size = it.size()
for (i in 0 until it.size()-1) {
output = output + it.get(i) + ","
}
output.dropLast(1)
rows.add(output)
}
return rows
}
Below is the exception I get.
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at trivago.ti.tools.FileProcessor.readCSVFile(FileProcessor.kt:16)
at trivago.ti.tools.ComparatorMainKt.main(ComparatorMain.kt:25)
I have the same logic executing in Java but it works fine. Below is what I have in Java.
private static List<String> readCSVFile(String filePath) throws IOException {
Reader in = new FileReader(filePath);
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
List<String> rows = new ArrayList<>();
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < record.size(); i++)
builder.append(record.get(i) + ",");
builder.deleteCharAt(builder.length() - 1);
rows.add(builder.toString());
}
return rows;
}
Why is Kotlin having a problem with this? Am I doing something wrong with the loop? Any help would be much appreciated as I'm a newbie to Kotlin.
java memory kotlin jvm out-of-memory
I am running the below loop in Kotlin and is throwing an out of memory error. I am running this for reading the rows in a csv file. Size of "records" is 6422.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = ""
records.forEach() {
val size = it.size()
for (i in 0 until it.size()-1) {
output = output + it.get(i) + ","
}
output.dropLast(1)
rows.add(output)
}
return rows
}
Below is the exception I get.
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.Arrays.copyOf(Arrays.java:3332)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
at java.lang.StringBuilder.append(StringBuilder.java:136)
at trivago.ti.tools.FileProcessor.readCSVFile(FileProcessor.kt:16)
at trivago.ti.tools.ComparatorMainKt.main(ComparatorMain.kt:25)
I have the same logic executing in Java but it works fine. Below is what I have in Java.
private static List<String> readCSVFile(String filePath) throws IOException {
Reader in = new FileReader(filePath);
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
List<String> rows = new ArrayList<>();
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < record.size(); i++)
builder.append(record.get(i) + ",");
builder.deleteCharAt(builder.length() - 1);
rows.add(builder.toString());
}
return rows;
}
Why is Kotlin having a problem with this? Am I doing something wrong with the loop? Any help would be much appreciated as I'm a newbie to Kotlin.
java memory kotlin jvm out-of-memory
java memory kotlin jvm out-of-memory
edited Nov 14 '18 at 14:34
mayooran
asked Nov 14 '18 at 14:29
mayooranmayooran
1,68432858
1,68432858
What size is the CSV you are reading? What's the -Xmx value of your JVM (if you defined any yourself) ?
– Sofo Gial
Nov 14 '18 at 14:33
Sorry I edited the question. its 6422
– mayooran
Nov 14 '18 at 14:34
In bytes? Also, did you define the -Xmx anywhere or is it default?
– Sofo Gial
Nov 14 '18 at 14:36
I guess the answers have solved the problem, so this is to solve another one: replacei in 0 until it.size()-1
withi in 0 until it.size()
ori in 0..it.size()-1
because the code as it is loses the last line.
– forpas
Nov 14 '18 at 14:58
add a comment |
What size is the CSV you are reading? What's the -Xmx value of your JVM (if you defined any yourself) ?
– Sofo Gial
Nov 14 '18 at 14:33
Sorry I edited the question. its 6422
– mayooran
Nov 14 '18 at 14:34
In bytes? Also, did you define the -Xmx anywhere or is it default?
– Sofo Gial
Nov 14 '18 at 14:36
I guess the answers have solved the problem, so this is to solve another one: replacei in 0 until it.size()-1
withi in 0 until it.size()
ori in 0..it.size()-1
because the code as it is loses the last line.
– forpas
Nov 14 '18 at 14:58
What size is the CSV you are reading? What's the -Xmx value of your JVM (if you defined any yourself) ?
– Sofo Gial
Nov 14 '18 at 14:33
What size is the CSV you are reading? What's the -Xmx value of your JVM (if you defined any yourself) ?
– Sofo Gial
Nov 14 '18 at 14:33
Sorry I edited the question. its 6422
– mayooran
Nov 14 '18 at 14:34
Sorry I edited the question. its 6422
– mayooran
Nov 14 '18 at 14:34
In bytes? Also, did you define the -Xmx anywhere or is it default?
– Sofo Gial
Nov 14 '18 at 14:36
In bytes? Also, did you define the -Xmx anywhere or is it default?
– Sofo Gial
Nov 14 '18 at 14:36
I guess the answers have solved the problem, so this is to solve another one: replace
i in 0 until it.size()-1
with i in 0 until it.size()
or i in 0..it.size()-1
because the code as it is loses the last line.– forpas
Nov 14 '18 at 14:58
I guess the answers have solved the problem, so this is to solve another one: replace
i in 0 until it.size()-1
with i in 0 until it.size()
or i in 0..it.size()-1
because the code as it is loses the last line.– forpas
Nov 14 '18 at 14:58
add a comment |
3 Answers
3
active
oldest
votes
Use a StringBuilder
in your kotlin code as well. You are creating a log of String
objects in heap. String is immutable and this code:
var output = ""
output = output + ","
is creating two objects in heap, although you have only reference to one of them. So the other one is eligible for GC to remove it. GC is "working" too hard in your case, that's why you get java.lang.OutOfMemoryError: GC overhead limit exceeded
.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = StringBuilder("")
records.forEach() {
output = StringBuilder("")
val size = it.size()
for (i in 0 until it.size()-1) {
output = output.append(it.get(i) + ",")
}
output.deleteCharAt(output.length - 1)
rows.add(output.toString())
}
return rows
}
You code will also run a lot faster, as creating a new object is quite costly.
add a comment |
I think there is a bug in your code
records.forEach() {
output = "" // clear output ;)
...
}
Compare it to your java code
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder(); // clear builder
...
}
Did you try this ? I don't think this will help, as OP is gettingGC overhead limit exceeded
, that means there are a lot of objects in heap
– Schidu Luca
Nov 14 '18 at 15:05
I agree that StringBuilder will help, but this will also help, because this is the cause of the GC having to run that many times. rows.add(output) -> rows.add("row1"); rows.add(output) -> rows.add("row1row2"); rows.add(output) -> rows.add("row1row2row3"); rows.add(output) -> rows.add("row1row2row3...row6422");
– Julio Daniel Reyes
Nov 14 '18 at 15:28
this does not affect in any way how many time GC runsrows.add(output)
, the string just keeps getting bigger, OP's mistake
– Schidu Luca
Nov 14 '18 at 15:37
The string keeps getting bigger, running out of heap memory, will cause the GC to run. GC has been trying to free the memory but is pretty much unable to get any job done, as rows content cannot be garbage collected.
– Julio Daniel Reyes
Nov 14 '18 at 15:44
add a comment |
You have two problems in your Kotlin code:
- You are using strings and string concatenation - which is costly operation. You should use StringBuilder as well.
- You are setting
output = ""
outside of the foreach loop - for each iteration, you have all previous rows inside output
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302540%2fout-of-memory-error-in-kotlin-when-looping-through-a-medium-size-data-set%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use a StringBuilder
in your kotlin code as well. You are creating a log of String
objects in heap. String is immutable and this code:
var output = ""
output = output + ","
is creating two objects in heap, although you have only reference to one of them. So the other one is eligible for GC to remove it. GC is "working" too hard in your case, that's why you get java.lang.OutOfMemoryError: GC overhead limit exceeded
.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = StringBuilder("")
records.forEach() {
output = StringBuilder("")
val size = it.size()
for (i in 0 until it.size()-1) {
output = output.append(it.get(i) + ",")
}
output.deleteCharAt(output.length - 1)
rows.add(output.toString())
}
return rows
}
You code will also run a lot faster, as creating a new object is quite costly.
add a comment |
Use a StringBuilder
in your kotlin code as well. You are creating a log of String
objects in heap. String is immutable and this code:
var output = ""
output = output + ","
is creating two objects in heap, although you have only reference to one of them. So the other one is eligible for GC to remove it. GC is "working" too hard in your case, that's why you get java.lang.OutOfMemoryError: GC overhead limit exceeded
.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = StringBuilder("")
records.forEach() {
output = StringBuilder("")
val size = it.size()
for (i in 0 until it.size()-1) {
output = output.append(it.get(i) + ",")
}
output.deleteCharAt(output.length - 1)
rows.add(output.toString())
}
return rows
}
You code will also run a lot faster, as creating a new object is quite costly.
add a comment |
Use a StringBuilder
in your kotlin code as well. You are creating a log of String
objects in heap. String is immutable and this code:
var output = ""
output = output + ","
is creating two objects in heap, although you have only reference to one of them. So the other one is eligible for GC to remove it. GC is "working" too hard in your case, that's why you get java.lang.OutOfMemoryError: GC overhead limit exceeded
.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = StringBuilder("")
records.forEach() {
output = StringBuilder("")
val size = it.size()
for (i in 0 until it.size()-1) {
output = output.append(it.get(i) + ",")
}
output.deleteCharAt(output.length - 1)
rows.add(output.toString())
}
return rows
}
You code will also run a lot faster, as creating a new object is quite costly.
Use a StringBuilder
in your kotlin code as well. You are creating a log of String
objects in heap. String is immutable and this code:
var output = ""
output = output + ","
is creating two objects in heap, although you have only reference to one of them. So the other one is eligible for GC to remove it. GC is "working" too hard in your case, that's why you get java.lang.OutOfMemoryError: GC overhead limit exceeded
.
fun readCSVFile(filePath: String): List<String> {
val reader = FileReader(filePath)
val records = CSVFormat.DEFAULT.parse(reader)
val rows = mutableListOf<String>()
var output = StringBuilder("")
records.forEach() {
output = StringBuilder("")
val size = it.size()
for (i in 0 until it.size()-1) {
output = output.append(it.get(i) + ",")
}
output.deleteCharAt(output.length - 1)
rows.add(output.toString())
}
return rows
}
You code will also run a lot faster, as creating a new object is quite costly.
edited Nov 14 '18 at 15:02
answered Nov 14 '18 at 14:49
Schidu LucaSchidu Luca
2,834520
2,834520
add a comment |
add a comment |
I think there is a bug in your code
records.forEach() {
output = "" // clear output ;)
...
}
Compare it to your java code
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder(); // clear builder
...
}
Did you try this ? I don't think this will help, as OP is gettingGC overhead limit exceeded
, that means there are a lot of objects in heap
– Schidu Luca
Nov 14 '18 at 15:05
I agree that StringBuilder will help, but this will also help, because this is the cause of the GC having to run that many times. rows.add(output) -> rows.add("row1"); rows.add(output) -> rows.add("row1row2"); rows.add(output) -> rows.add("row1row2row3"); rows.add(output) -> rows.add("row1row2row3...row6422");
– Julio Daniel Reyes
Nov 14 '18 at 15:28
this does not affect in any way how many time GC runsrows.add(output)
, the string just keeps getting bigger, OP's mistake
– Schidu Luca
Nov 14 '18 at 15:37
The string keeps getting bigger, running out of heap memory, will cause the GC to run. GC has been trying to free the memory but is pretty much unable to get any job done, as rows content cannot be garbage collected.
– Julio Daniel Reyes
Nov 14 '18 at 15:44
add a comment |
I think there is a bug in your code
records.forEach() {
output = "" // clear output ;)
...
}
Compare it to your java code
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder(); // clear builder
...
}
Did you try this ? I don't think this will help, as OP is gettingGC overhead limit exceeded
, that means there are a lot of objects in heap
– Schidu Luca
Nov 14 '18 at 15:05
I agree that StringBuilder will help, but this will also help, because this is the cause of the GC having to run that many times. rows.add(output) -> rows.add("row1"); rows.add(output) -> rows.add("row1row2"); rows.add(output) -> rows.add("row1row2row3"); rows.add(output) -> rows.add("row1row2row3...row6422");
– Julio Daniel Reyes
Nov 14 '18 at 15:28
this does not affect in any way how many time GC runsrows.add(output)
, the string just keeps getting bigger, OP's mistake
– Schidu Luca
Nov 14 '18 at 15:37
The string keeps getting bigger, running out of heap memory, will cause the GC to run. GC has been trying to free the memory but is pretty much unable to get any job done, as rows content cannot be garbage collected.
– Julio Daniel Reyes
Nov 14 '18 at 15:44
add a comment |
I think there is a bug in your code
records.forEach() {
output = "" // clear output ;)
...
}
Compare it to your java code
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder(); // clear builder
...
}
I think there is a bug in your code
records.forEach() {
output = "" // clear output ;)
...
}
Compare it to your java code
for (CSVRecord record : records) {
StringBuilder builder = new StringBuilder(); // clear builder
...
}
edited Nov 14 '18 at 15:03
answered Nov 14 '18 at 14:41
Julio Daniel ReyesJulio Daniel Reyes
2,082815
2,082815
Did you try this ? I don't think this will help, as OP is gettingGC overhead limit exceeded
, that means there are a lot of objects in heap
– Schidu Luca
Nov 14 '18 at 15:05
I agree that StringBuilder will help, but this will also help, because this is the cause of the GC having to run that many times. rows.add(output) -> rows.add("row1"); rows.add(output) -> rows.add("row1row2"); rows.add(output) -> rows.add("row1row2row3"); rows.add(output) -> rows.add("row1row2row3...row6422");
– Julio Daniel Reyes
Nov 14 '18 at 15:28
this does not affect in any way how many time GC runsrows.add(output)
, the string just keeps getting bigger, OP's mistake
– Schidu Luca
Nov 14 '18 at 15:37
The string keeps getting bigger, running out of heap memory, will cause the GC to run. GC has been trying to free the memory but is pretty much unable to get any job done, as rows content cannot be garbage collected.
– Julio Daniel Reyes
Nov 14 '18 at 15:44
add a comment |
Did you try this ? I don't think this will help, as OP is gettingGC overhead limit exceeded
, that means there are a lot of objects in heap
– Schidu Luca
Nov 14 '18 at 15:05
I agree that StringBuilder will help, but this will also help, because this is the cause of the GC having to run that many times. rows.add(output) -> rows.add("row1"); rows.add(output) -> rows.add("row1row2"); rows.add(output) -> rows.add("row1row2row3"); rows.add(output) -> rows.add("row1row2row3...row6422");
– Julio Daniel Reyes
Nov 14 '18 at 15:28
this does not affect in any way how many time GC runsrows.add(output)
, the string just keeps getting bigger, OP's mistake
– Schidu Luca
Nov 14 '18 at 15:37
The string keeps getting bigger, running out of heap memory, will cause the GC to run. GC has been trying to free the memory but is pretty much unable to get any job done, as rows content cannot be garbage collected.
– Julio Daniel Reyes
Nov 14 '18 at 15:44
Did you try this ? I don't think this will help, as OP is getting
GC overhead limit exceeded
, that means there are a lot of objects in heap– Schidu Luca
Nov 14 '18 at 15:05
Did you try this ? I don't think this will help, as OP is getting
GC overhead limit exceeded
, that means there are a lot of objects in heap– Schidu Luca
Nov 14 '18 at 15:05
I agree that StringBuilder will help, but this will also help, because this is the cause of the GC having to run that many times. rows.add(output) -> rows.add("row1"); rows.add(output) -> rows.add("row1row2"); rows.add(output) -> rows.add("row1row2row3"); rows.add(output) -> rows.add("row1row2row3...row6422");
– Julio Daniel Reyes
Nov 14 '18 at 15:28
I agree that StringBuilder will help, but this will also help, because this is the cause of the GC having to run that many times. rows.add(output) -> rows.add("row1"); rows.add(output) -> rows.add("row1row2"); rows.add(output) -> rows.add("row1row2row3"); rows.add(output) -> rows.add("row1row2row3...row6422");
– Julio Daniel Reyes
Nov 14 '18 at 15:28
this does not affect in any way how many time GC runs
rows.add(output)
, the string just keeps getting bigger, OP's mistake– Schidu Luca
Nov 14 '18 at 15:37
this does not affect in any way how many time GC runs
rows.add(output)
, the string just keeps getting bigger, OP's mistake– Schidu Luca
Nov 14 '18 at 15:37
The string keeps getting bigger, running out of heap memory, will cause the GC to run. GC has been trying to free the memory but is pretty much unable to get any job done, as rows content cannot be garbage collected.
– Julio Daniel Reyes
Nov 14 '18 at 15:44
The string keeps getting bigger, running out of heap memory, will cause the GC to run. GC has been trying to free the memory but is pretty much unable to get any job done, as rows content cannot be garbage collected.
– Julio Daniel Reyes
Nov 14 '18 at 15:44
add a comment |
You have two problems in your Kotlin code:
- You are using strings and string concatenation - which is costly operation. You should use StringBuilder as well.
- You are setting
output = ""
outside of the foreach loop - for each iteration, you have all previous rows inside output
add a comment |
You have two problems in your Kotlin code:
- You are using strings and string concatenation - which is costly operation. You should use StringBuilder as well.
- You are setting
output = ""
outside of the foreach loop - for each iteration, you have all previous rows inside output
add a comment |
You have two problems in your Kotlin code:
- You are using strings and string concatenation - which is costly operation. You should use StringBuilder as well.
- You are setting
output = ""
outside of the foreach loop - for each iteration, you have all previous rows inside output
You have two problems in your Kotlin code:
- You are using strings and string concatenation - which is costly operation. You should use StringBuilder as well.
- You are setting
output = ""
outside of the foreach loop - for each iteration, you have all previous rows inside output
answered Nov 14 '18 at 15:43
deydey
783716
783716
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302540%2fout-of-memory-error-in-kotlin-when-looping-through-a-medium-size-data-set%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What size is the CSV you are reading? What's the -Xmx value of your JVM (if you defined any yourself) ?
– Sofo Gial
Nov 14 '18 at 14:33
Sorry I edited the question. its 6422
– mayooran
Nov 14 '18 at 14:34
In bytes? Also, did you define the -Xmx anywhere or is it default?
– Sofo Gial
Nov 14 '18 at 14:36
I guess the answers have solved the problem, so this is to solve another one: replace
i in 0 until it.size()-1
withi in 0 until it.size()
ori in 0..it.size()-1
because the code as it is loses the last line.– forpas
Nov 14 '18 at 14:58