0

Here's what I thought:

  1. JVM copy the string from file system into main memory.
  2. JVM copy the string from main memory into Java heap.
  3. Use it.

An I right ? I mean, there's actually two step copy.

WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
  • 2
    What is the distinction between "main memory" and "Java heap"? – Oliver Charlesworth Jun 26 '13 at 03:14
  • Actually I don't quite understand, I've thought JVM could operate main memory directly, but from [this](http://stackoverflow.com/questions/17310565/filechannel-transferfroms-comments-explanation/17310713?noredirect=1#17310713) answer I'm confused. – WoooHaaaa Jun 26 '13 at 03:15
  • The JVM does not actually read directly; it has to convert the bytes into its internal String representation in any case! – fge Jun 26 '13 at 03:18
  • @fge, That's means, JVM actually need to copy twice, is it ? since the convert also needs copy. – WoooHaaaa Jun 26 '13 at 03:20
  • What is the code you use to read the string? – Marichyasana Jun 26 '13 at 03:22
  • That does not mean it copies the _full bytes_ before converting; it converts as it reads. – fge Jun 26 '13 at 03:22
  • @Marichyasana Just normal InputStream stuff. – WoooHaaaa Jun 26 '13 at 03:28
  • @fge , so actually how many times copy ? – WoooHaaaa Jun 26 '13 at 03:28
  • Well, once: when the JVM allocates the buffer to write the `String` in its native format; also, the buffer it allocates to read the file when using an `InputStream`. But this buffer never covers the _full_ file. – fge Jun 26 '13 at 03:32
  • Thanks @fge, Could you answer this question instead of comment and have a look at [this](http://stackoverflow.com/questions/17310565/filechannel-transferfroms-comments-explanation/17310713?noredirect=1#17310713) answer, according your explanation ,he seems to be wrong ? – WoooHaaaa Jun 26 '13 at 03:36
  • This answer does not cover the same scenario AFAICS, the OP asked about how `FileChannel`'s .transfer*() methods worked. Here you are talking about reading bytes from a file and make them into a String. Quite not the same thing ;) – fge Jun 26 '13 at 03:41

2 Answers2

1

veaThere could be more than 2 copies. Very much depends on how you are reading.

Consider the common case of a FileReader wrapped in a BufferedReader.

When you call BufferedReader.readLine() you get three copies.

1) The BufferedReader is empty (to start) so it call read(char[]) on the FileReader.

2) The FileReader (at the C layer of the JVM) make a read() system call into a uint8[] buffer. (copy 1)

3) Best case FileReader then converts the unit8[] contents and copies the result into the char[] provided by the BufferedReader (copy 2). (Note this copy would still be present even if we have an InputStreams and the result was a byte[] instead of a string.)

4) The readLine() then copies the char[] up to the end of the line into a String. (copy 3).

For most things you don't need to worry about all of the copying. The buffers are small and the overhead is minimal.

Rob

Rob Moore
  • 3,343
  • 17
  • 18
0

When a Java program started Java Virtual Machine gets some memory from Operating System.

Java Virtual Machine or JVM uses this memory for all its need and part of this memory is call java heap memory.

Heap in Java generally located at bottom of address space and move upwards.

Whenever we create object using new operator or by any another means object is allocated memory from Heap and When object dies or garbage collected ,memory goes back to Heap space in Java.

Java Heap could be in the main memory (RAM) or in disk or both depending on your operating system configuration.

arifnpm
  • 357
  • 1
  • 7