npsonic Active Member Licensed User Nov 20, 2020 #1 This code will give exception java.lang.OutOfMemoryError: Java heap space How is that possible or do I not understand something? B4X: Copy Dim s As String = File.ReadString(File.DirApp, "lorem.txt") Do While s.Length > 0 s = s.SubString(1) Loop File used for test is just 100 KB of lorem ipsum
This code will give exception java.lang.OutOfMemoryError: Java heap space How is that possible or do I not understand something? B4X: Copy Dim s As String = File.ReadString(File.DirApp, "lorem.txt") Do While s.Length > 0 s = s.SubString(1) Loop File used for test is just 100 KB of lorem ipsum
stevel05 Expert Licensed User Longtime User Nov 20, 2020 #2 Strings are Immutable, therefore every time you make a change to it, it creates a new string object. I would guess that creating 100000 new string objects in short order is overwhelming the garbage collector as there is no time in between the iterations for it to run. Upvote 0
Strings are Immutable, therefore every time you make a change to it, it creates a new string object. I would guess that creating 100000 new string objects in short order is overwhelming the garbage collector as there is no time in between the iterations for it to run.
stevel05 Expert Licensed User Longtime User Nov 20, 2020 #3 If you really need to do something like that, try it with stringbuilder. Upvote 0
AnandGupta Expert Licensed User Longtime User Nov 20, 2020 #4 @npsoni , you are increasing the string s length infinately So it is showing heap error, obviously. Regards Anand Upvote 0
@npsoni , you are increasing the string s length infinately So it is showing heap error, obviously. Regards Anand
Erel B4X founder Staff member Licensed User Longtime User Nov 22, 2020 #5 Moved to the questions forum. I'm not sure what you are trying to do but StringBuilder is the answer. Upvote 0
Moved to the questions forum. I'm not sure what you are trying to do but StringBuilder is the answer.