文章;MemoryStream.Close() or MemoryStream.Dispose()

Close() and Dispose(), when called on a MemoryStream, only serve to do two things:

  • Mark the object disposed so that future accidental usage of the object will throw an exception.
  • Possibly1 release references to managed objects, which can make the GC's job a bit easier depending on the GC implementation. (On today's GC algorithms it makes no real difference, so this is a point for an academic discussion and has no significant real-world impact.)

MemoryStream does not have any unmanaged resources to dispose, so you don't technically have to dispose of it. The effect of not disposing a MemoryStream is roughly the same thing as dropping a reference to a byte[] -- the GC will clean both up the same way.

Which one do I call? Is it necessary to call both?

The Dispose() method of streams delegate directly to the Close() method2, so both do exactly the same thing.

Will the other throw an exception if I have already called one of them?

The documentation for IDisposable.Dispose() specifically states it is safe to call Dispose() multiple times, on any object3. (If that is not true for a particular class then that class implements the IDisposable interface in a way that violates its contract, and this would be a bug.)

All that to say: it really doesn't make a huge difference whether you dispose a MemoryStream or not. The only real reason it has Close/Dispose methods is because it inherits from Stream, which requires those methods as part of its contract to support streams that do have unmanaged resources (such as file or socket descriptors).

流Stream的关闭的更多相关文章

  1. nodeJS之流stream

    前面的话 当内存中无法一次装下需要处理的数据时,或者一边读取一边处理更加高效时,我们就需要用到数据流.NodeJS中通过各种Stream来提供对数据流的操作.本文将详细说明NodeJS中的流strea ...

  2. Java笔记:Java 流(Stream)、文件(File)和IO

    更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...

  3. Java - 17 Java 流(Stream)、文件(File)和IO

    Java 流(Stream).文件(File)和IO Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型. ...

  4. Java总结:Java 流(Stream)、文件(File)和IO

    更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...

  5. 流Stream 文件File 流IO

    Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...

  6. 20180803-Java 流(Stream)、文件(File)和IO

    Java 流(Stream).文件(File)和IO 下面的程序示范了用read()方法从控制台不断读取字符直到用户输入"q". // 使用BufferedReader 在控制台读 ...

  7. Java 流(Stream)、文件(File)和IO

    Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序 ...

  8. mybatis oracle java.sql.SQLException: 流已被关闭问题

    /** * 按照页码取值(从current_page页开始,每页page_size条) * @param key * @param params * @param current_page * @pa ...

  9. JAVA基础知识之IO——IO流(Stream)的概念

    Java IO 流 Java将不同的设备或载体(键盘.文件.网络.管道等)的输入输出数据统称为"流"(Stream),即JAVA的IO都是基于流的. JAVA传统的所有流类型类都包 ...

随机推荐

  1. php-resque 队列简单使用

    一.安装 php-resque 进入项目根目录,composer 安装 php-resque composer require chrisboulton/php-resque 二.常用方法 1.连接 ...

  2. Windows10 下安装 oracle 客户端,安装 plsql 破解并实现汉化

    一,软件准备 1,win10 操作系统 2,oracle_11g_r2 client 这里是 64 位的软件  3, plsql 11.0.6 这里我们下载 64 的,32 位操作系统现在已经很少了, ...

  3. JS增删改查localStorage实现搜索历史功能

    <script type="text/javascript"> var referrerPath = "@ViewBag.ReferrerPath" ...

  4. 【视频开发】用GStreamer实现摄像头的采集和保存

    GStreamer是流媒体软件的开发框架.可以这样说,在该框架的支持下,你可以非常简单地为很多格式的流媒体写出自已需要的程序. 现在,GStreamer已经内置对MP3.Ogg/Vorbis.MPEG ...

  5. Vue脚手架菜鸟级错误 npm run dev

    ERROR Failed to compile with 1 errors Failed to resolve loader: stylus-loaderYou may need to install ...

  6. TomatoLog 是一个基于 .NETCore 平台的产品。

    TomatoLog TomatoLog 是一个基于 .NETCore 平台的产品. The TomatoLog 是一个中间件,包含客户端.服务端,非常容易使用和部署. 客户端实现了ILoggerFac ...

  7. 小甲鱼汇编语言学习笔记——day01

    1.计算机CPU由如下三部分组成(如下图):运算器.控制器.寄存器. 2.寄存器:简单说,就是用来存储数据的器件,类似内存,不过这个是CPU专用,跟内存不一样. 8086CPU里,寄存器总共有14个, ...

  8. centos6 rpm安装mysql 5.7

    安装过程中需要进行系统补包操作 [root@cent6zbx ~]# rpm -qa |grep mysql mysql-libs--.el6_6.x86_64 [root@cent6zbx ~]# ...

  9. 解析Java反射java.lang.IllegalArgumentException: wrong number of arguments

    项目中遇到的问题 import org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; public cl ...

  10. netcore 版本 切换 sdk

    https://docs.microsoft.com/zh-cn/dotnet/core/tools/global-json