Task | Methods or properties to use |
---|---|
To obtain a MemoryMappedFile object that represents a persisted memory-mapped file from a file on disk. | MemoryMappedFile.CreateFromFile method. |
To obtain a MemoryMappedFile object that represents a non-persisted memory-mapped file (not associated with a file on disk). | MemoryMappedFile.CreateNew method. - or - MemoryMappedFile.CreateOrOpen method. |
To obtain a MemoryMappedFile object of an existing memory-mapped file (either persisted or non-persisted). | MemoryMappedFile.OpenExisting method. |
To obtain a UnmanagedMemoryStream object for a sequentially accessed view to the memory-mapped file. | MemoryMappedFile.CreateViewStream method. |
To obtain a UnmanagedMemoryAccessor object for a random access view to a memory-mapped fie. | MemoryMappedFile.CreateViewAccessor method. |
To obtain a SafeMemoryMappedViewHandle object to use with unmanaged code. | MemoryMappedFile.SafeMemoryMappedFileHandle property. - or - MemoryMappedViewAccessor.SafeMemoryMappedViewHandle property. - or - MemoryMappedViewStream.SafeMemoryMappedViewHandle property. |
To delay allocating memory until a view is created (non-persisted files only). (To determine the current system page size, use the Environment.SystemPageSize property.) | CreateNew method with the MemoryMappedFileOptions.DelayAllocatePages value. - or - CreateOrOpen methods that have a MemoryMappedFileOptions enumeration as a parameter. |
Process A
creates the memory-mapped file and writes a value to it.Process B
opens the memory-mapped file and writes a value to it.Process C
opens the memory-mapped file and writes a value to it.Process A
reads and displays the values from the memory-mapped file.Process A
is finished with the memory-mapped file, the file is immediately reclaimed by garbage collection.Process A
.Process B
.Process A
and press ENTER.Process C
.Process A
and press ENTER.Process A
is as follows:SeekableByteChannel
that is connected to a file. It has a current position within its file which can be both queried
and modified
. The file itself contains a variable-length sequence of bytes that can be read and written and whose current size
can be queried. The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is truncated. The file may also have some associated metadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access. read
or written
at an absolute position in a file in a way that does not affect the channel's current position. mapped
directly into memory; for large files this is often much more efficient than invoking the usual read or write methods. forced out
to the underlying storage device, ensuring that data are not lost in the event of a system crash. to some other channel
, and vice versa
, in a way that can be optimized by many operating systems into a very fast transfer directly to or from the filesystem cache. locked
against access by other programs. close
method may be invoked at any time, as specified by the Channel
interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified. open
methods defined by this class. A file channel can also be obtained from an existing FileInputStream
, FileOutputStream
, or RandomAccessFile
object by invoking that object's getChannel method, which returns a file channel that is connected to the same underlying file. Where the file channel is obtained from an existing stream or random access file then the state of the file channel is intimately connected to that of the object whose getChannel method returned the channel. Changing the channel's position, whether explicitly or by reading or writing bytes, will change the file position of the originating object, and vice versa. Changing the file's length via the file channel will change the length seen via the originating object, and vice versa. Changing the file's content by writing bytes will change the content seen by the originating object, and vice versa. getChannel
method of a FileInputStream
instance will be open for reading. A channel obtained via the getChannel
method of a FileOutputStream
instance will be open for writing. Finally, a channel obtained via the getChannel
method of a RandomAccessFile
instance will be open for reading if the instance was created with mode 'r' and will be open for reading and writing if the instance was created with mode 'rw'. FileOutputStream(File,boolean)
constructor and passing true for the second parameter. In this mode each invocation of a relative write operation first advances the position to the end of the file and then writes the requested data. Whether the advancement of the position and the writing of the data are done in a single atomic operation is system-dependent and therefore unspecified.