user974270
user974270

Reputation: 627

java read 200mb file, when load in memory, how much memory will need?

I have a file with 200mb, the file with following format

each line is a spaced 0,1,2,3 integer.

0 1 2 0 1 3...

So, if I want to read this file into memory, should I be using array, using Vector<Integer>? or using ByteArray? or String?

How can I estimate the memory cost, so that can I set the java heap size appropriately? Would 256mb be enough?

Upvotes: 1

Views: 1520

Answers (1)

Andrzej Jozwik
Andrzej Jozwik

Reputation: 14649

For String size x 2 - String use char for each byte (you can set virtual machine flag to use byte)

-XX:+UseCompressedStrings   
   Use a byte[] for Strings which can be represented as pure ASCII. 
   (Introduced in Java 6 Update 21 Performance Release)

For List - count how many integer. For each integer it create one pointer to int (4 bytes or 8 bytes dependence on platform - you can set jvm option to use 32 bit address on 64 bit machine -XX:+UseCompressedOops)

Number of integers x ((size of one Integer) + (pointer to Integer in list)) = 2 * (number of integers) * (size of one pointer)

ByteBuffer - the same size like file

The best solution is primitive array - byte, short or int. It dependents of integer precision. For byte you have (number of int) bytes - can be less than file size.

Upvotes: 1

Related Questions