You simply are not in memory, the file is read correctly. As already mentioned, the first option is to see where you can free some heap heap in your application. However, if you have a slightly larger file, it may break again.
You need to answer this question: why do I need all the text at once in my memory? And if you do not find the final point for this, you will need to answer the following question: how will I do my algorithm / processing / display of the file in pieces, so I only need to save part of the file in memory.
With more information on what you need to do with this text, you will get more information on how to do it differently if you need to.
Edit:
instead of displaying the entire contents of the file immediately in the editing text, you can, for example, show pages: read part of the file (say, for example, 100 lines). Once the user reaches the bottom of the current page, load the next 100 lines from the file (and, of course, discard the previous 100).
You can do this with ViewPager (each fragment shows 100 lines, swipe left to display the next page of text, ...)
For the first time, you can view the entire file to calculate the total number of lines (without saving the text) and, thus, indicate the number of pages that will do.
source share