I know a char in java is 2bytes. but if load some chars in a list, they will cost 87B to save a char. the test is like the following:
There is a file “source” containing 995328 lines. every line is just a character:’a’. (so it will cost almost 2MB in java to save all chars).
There are two sleep method called in my source code and I use top command to check the memory usage at any moment.
The RSIZE value when running the first sleep(10000) method is 25M, and 108M when running the second sleep method. so per String(which is just an “a” ) cost:(108MB-25MB)/995328=87B. I don’t know why a String “a” cost so many memory!!! Could any one can tell my why?
public static void main(String[] args) throws Exception{
File file = new File("source");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
List<String> list = new ArrayList<String>();
Thread.sleep(10000);
while((line = br.readLine())!=null){
list.add(line);
}
Thread.sleep(10000);
}
@Amir is right to say that there are better ways than top (for instance, hprof is included with the JDK) to measure memory usage, but there are some deeper issues that confound your memory numbers.
fileorbr. This is the big one. Each one of these objects is a wrapper around a bunch of native code to interface with your operating system’s file I/O libraries. Those resources include file handles and cached buffers, so some of the data you’re reading from the file is counted twice in memory usage – once in the cache attached tobr, and once inlist.listvariable has overhead as well. There is a backing array, where each slot is a pointer (8 more bytes), and there are plenty of empty slots. As the backing array grows to accommodate the lines, theArrayListclass leaves some extra space because the array resize (that is, create a new array and copy over all the elements from the old array) is expensive, and each of the empty slots is 8 bytes on a 64-bit system.ArrayListare quite likely still in memory, and counting towards the top numbers. Since those arrays are large to start with (most likely there is one that is at least 500K slots, each an 8-byte pointer), this boosts the program’s total memory usage.N.B. I talked about 8-byte pointers above, assuming a 64-bit system. On a 32-bit system, everything I said holds, except that pointers are only 4 bytes.