Java - ArrayList <String> limit

I am trying to read a text file and save each line in an ArrayList However, the text file is too long (about 2,000,000) lines and an error: java.lang.OutOfMemoryError occurs.

How do I know if the arrailist is full, and then create another array to save the remaining data automatically?

Sorry for my bad english.

Thank you for your help.

+4
source share
4 answers

2 million rows far exceed the maximum size for the Java collection (INTEGER.MAX_VALUE or 2 billion indexes).

Most likely you will get the error "outOfMemory". You can do either

  • JVM.

java -Xmx4g

4g = 4 .

192 , - 1 .

http://www.oracle.com/technetwork/java/javase/6u18-142093.html

  1. konsolas, .

, !

+1

, ArrayList . , ( Integer.MAX_VALUE - ). , , , ArrayList.

1: ( )

try..catch. OutOfMemoryError, , . , . . ! ...

, OutOfMemoryError . , - .

2:

. , , 1/3 . . 1/3 .

, clear(), . , (1/3 ) !

0

, . , , , .

, , , . :

  • 1000
  • / ..
  • 1000
  • .

ArrayList 2 147 483 647 . (max int)

0

, , , ArrayList . - , BigArrayList . ArrayList . : , .

0

Source: https://habr.com/ru/post/1617885/


All Articles