1

I'm having a file containing one single line data. And the size of file is 300 MB.

This file's data am reading whole as one string and storing into one string variable. Then I'm using that variable for my lookups.

So my question is Is java string variable can hold this much heavy string ?

I think memory issue can happen ..but need suggestions.

Please give your suggestions.

Thanks !!

kelly
  • 243
  • 3
  • 12
  • 1
    Suggestions for what? It is not clear what you are asking at all. – rmlan Dec 07 '16 at 18:29
  • 2
    You'll end up with 2 bytes per UTF-16 code unit... if your file is all ASCII for example, you'll have a 600MB string in memory (at best - depending on how you're reading the file, the underlying `char[]` could easily be bigger, in the same way that an `ArrayList` has spare capacity usually). – Jon Skeet Dec 07 '16 at 18:30
  • Yes, a Java String can store a String this large. Yes, you may run out of heap space. There are command line options that let you specify a maximum amount of heap space when you start the JVM. Is that what you're looking for? – Dawood ibn Kareem Dec 07 '16 at 18:30
  • My answer to this question http://stackoverflow.com/a/41024966/57695 – Peter Lawrey Dec 07 '16 at 18:45

1 Answers1

3

Java strings can only hold 2147483647 (2^31 - 1) characters (depending on your JVM). So if your string is bigger than that, you will have to break up the string. You could break up the string and store each value in an array, or a hash map, or any viable data structure.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156