流Stream的关闭
文章;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的关闭的更多相关文章
- nodeJS之流stream
前面的话 当内存中无法一次装下需要处理的数据时,或者一边读取一边处理更加高效时,我们就需要用到数据流.NodeJS中通过各种Stream来提供对数据流的操作.本文将详细说明NodeJS中的流strea ...
- Java笔记:Java 流(Stream)、文件(File)和IO
更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...
- Java - 17 Java 流(Stream)、文件(File)和IO
Java 流(Stream).文件(File)和IO Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型. ...
- Java总结:Java 流(Stream)、文件(File)和IO
更新时间:2018-1-7 12:27:21 更多请查看在线文集:http://android.52fhy.com/java/index.html java.io 包几乎包含了所有操作输入.输出需要的 ...
- 流Stream 文件File 流IO
Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...
- 20180803-Java 流(Stream)、文件(File)和IO
Java 流(Stream).文件(File)和IO 下面的程序示范了用read()方法从控制台不断读取字符直到用户输入"q". // 使用BufferedReader 在控制台读 ...
- Java 流(Stream)、文件(File)和IO
Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序 ...
- mybatis oracle java.sql.SQLException: 流已被关闭问题
/** * 按照页码取值(从current_page页开始,每页page_size条) * @param key * @param params * @param current_page * @pa ...
- JAVA基础知识之IO——IO流(Stream)的概念
Java IO 流 Java将不同的设备或载体(键盘.文件.网络.管道等)的输入输出数据统称为"流"(Stream),即JAVA的IO都是基于流的. JAVA传统的所有流类型类都包 ...
随机推荐
- 工控随笔_23_WinCC报Application CCArchiveConnMon.exe could not establish a connection to the Win CC license server
西门子的软件都是比较庞大的,安装一个软件要很长时间,而且安装的过程中很容易因为系统兼容性问题导致不能成功安装的情况, 而有时还会出现安装成功,但是不能正常使用的情况.这个时候就很麻烦,一般除了重装系统 ...
- java基础:
java基础: 内容. 待续........
- PHP ob_gzhandler的理解
PHP ob_gzhandler的理解那么对于我们这些没有开启mod_deflate模块的主机来说,就只能采用ob_gzhandler函数来压缩了,它的压缩效果和mod_deflate相比,相差很小, ...
- java修饰符的权限范围
java有四个修饰符,分别为public/protected/default/private,这四个修饰符的权限范围是不一样的. public修饰的成员,在同类.同包.子类(继承自本类).其他包中都可 ...
- cf-786B区间图最短路
https://www.cnblogs.com/31415926535x/p/11611801.html 偶然看到的这个东西,可以说是第一次见到图论+数据结构的题了,,这题代码很简单,细节处理一下就没 ...
- 山峰和山谷 Ridges and Valleys
题目描述 思路 一开始看这道题目,也不是很会,谁会把统计之类的问题和bfs联系在一起,没有开始的状态,没有结束的状态,题目中连一个最短之类的词也没有出现. 然后统计嘛,题目中说了方格高度都相同,就把周 ...
- C语言提高内容目录
(1)基础 数据类型和变量 内存四区(栈 堆 全局 代码区) (2)指针和字符串操作 指针强化 字符串的基本操作 字符串一级指针内存模型图 字符串做为函数参数 (3)二级指针多级指针 二级指针的输入输 ...
- MongoDB 概述
一.概述: 1.NoSQL数据库(非关系型数据库) 2.文档存储 3.格式类似JSON,BSON 4.最终一致性(非ACID) , CAP定理(C 一致性,A 高可用,P 分区性) 5.高可扩展性(分 ...
- elasticsearch安全重启节点
elasticsearch集群,有时候可能需要修改配置,增加硬盘,扩展内存等操作,需要对节点进行维护升级.但是业务不能停,如果直接kill掉节点,可能导致数据丢失.而且集群会认为该节点挂掉了,就开始转 ...
- sql server 压缩日志文件
USE [master] GO ALTER DATABASE TestDB SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE TestDB SET ...