File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis.

Slides:



Advertisements
Similar presentations
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
Advertisements

Lecture 10: Heap Management CS 540 GMU Spring 2009.
CMSC 330: Organization of Programming Languages Memory and Garbage Collection.
DATA STRUCTURES Lecture: Initialization & Cleanup Slides adapted from Prof. Steven Roehrig.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
User-Level Memory Management in Linux Programming
Garbage Collection CSCI 2720 Spring Static vs. Dynamic Allocation Early versions of Fortran –All memory was static C –Mix of static and dynamic.
CPSC 388 – Compiler Design and Construction
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Memory Management. History Run-time management of dynamic memory is a necessary activity for modern programming languages Lisp of the 1960’s was one of.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
This presentation: Sasha GoldshteinCTO, Sela Group Garbage Collection Performance Tips.
File Management Systems
Processes CSCI 444/544 Operating Systems Fall 2008.
Generational Stack Collection And Profile driven Pretenuring Perry Cheng Robert Harper Peter Lee Presented By Moti Alperovitch
Incremental Garbage Collection
Chapter 3.7 Memory and I/O Systems. 2 Memory Management Only applies to languages with explicit memory management (C or C++) Memory problems are one of.
Linked lists and memory allocation Prof. Noah Snavely CS1114
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
1 Overview Assignment 6: hints  Living with a garbage collector Assignment 5: solution  Garbage collection.
SEG Advanced Software Design and Reengineering TOPIC L Garbage Collection Algorithms.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Intro to C++ And Some Tools Opening Discussion zHave any questions come up since last class? Have you had a chance to look over the project.
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Stack and Heap Memory Stack resident variables include:
Initialization & Cleanup Guaranteed initialization with the constructor The name of the constructor is the same as the name of the class.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Effective C# 50 Specific Ways to Improve Your C# Item 14~ /08/14 1.
C++ Memory Overview 4 major memory segments Key differences from Java
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Copyright Curt Hill Variables What are they? Why do we need them?
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Constructors & Garbage Collection Ch. 9 – Head First Java.
Garbage Collection and Memory Management CS 480/680 – Comparative Languages.
UniProcessor Garbage Collection Techniques Paul R. Wilson University of Texas Presented By Naomi Sapir Tel-Aviv University.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
C++ 程序语言设计 Chapter 12: Dynamic Object Creation. Outline  Object creation process  Overloading new & delete.
Object Oriented Software Development 4. C# data types, objects and references.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
® July 21, 2004GC Summer School1 Cycles to Recycle: Copy GC Without Stopping the World The Sapphire Collector Richard L. Hudson J. Eliot B. Moss Originally.
CS412/413 Introduction to Compilers and Translators April 21, 1999 Lecture 30: Garbage collection.
.NET Garbage Collection Performance Tips Sasha Goldshtein | SELA Group.
FASTFAST All rights reserved © MEP Make programming fun again.
Module 9: Memory and Resource Management
Process Management Process Concept Why only the global variables?
Concepts of programming languages
Dynamically Allocated Memory
CSC 253 Lecture 8.
Storage.
Simulated Pointers.
CSC 253 Lecture 8.
Memory Management and Garbage Collection Hal Perkins Autumn 2011
Simulated Pointers.
Strategies for automatic memory management
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Lecture Topics: 11/1 General Operating System Concepts Processes
Created By: Asst. Prof. Ashish Shah, J.M.Patel College, Goregoan West
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

File I/O Applied Component-Based Software Engineering File I/O CSE 668 / ECE 668 Prof. Roger Crawfis

Streams Streams are used anytime your program reads or writes a file, connects to another computer over a network, or generally does anything where it sends or receives bytes from one place to another.

Streams to read & write data Let’s say you have a simple program – a form with an event handler that needs to read data from a file. You’ll use a Stream object to do it. And if your program needs to write data out to the file, it can use another Stream object.

Different streams read & write different things

A FileStream writes bytes to a file

Using a FileStream

StreamWriter manages a FileStream 1. Use the StreamWriter’s constructor to open or create a file. StreamWriter writer = new true); 2. Use the Write() and WriteLine() methods to write to the file. writer.WriteLine("The {0} is set to {1} degrees.", appliance, temp.ToString()); 3. Call the Close() method to release the file. writer.Close();

Reading and Writing takes 2 objects

Make sure that you close streams! Be sure to close every steam that you open, even if you are just reading a file. Forgetting to close a stream is a big deal. If a stream is not closed, the file will be locked, and other programs can’t use that file until you close your stream.

Data can go through many streams You can chain streams. One stream can write to another stream, which writes to another stream… often ending with a network or file stream.

Built-in classes for Files & Directories Like StreamWriter, the File class creates streams for you to work with files behind the scenes. Likewise the Directory class lets you work with whole directories of files. FileInfo instance for doing a lot of work with a particular file. File class & static methods for a small number of actions.

File Support 1. Find out if it exists: Exists() method returns true or false. 2. Read from and write to the file: OpenRead() to get data, Create() or OpenWrite() to write to the file. 3. Append text to the file: AppendAllText() to append text to existing file. 4. Get information about the file: GetLastAccessTime() and GetLastWriteTime()

IDisposable for clean-up Many.Net classes implement the interface called IDisposable. This interface has only one member, the Dispose() method. When a class implements IDisposable, that means there are important things that it needs to do in order to shut down. Usually that is because it has allocated resources that it needs to release.

Using statements Anytime that you use a stream, you should always declare it inside a using statement. When you declare an object in a using block, that object’s Dispose() method is called automatically.

Using statements Multiple using statements can be put together with one set of curly braces.

Garbage Collection Applied Component-Based Software Engineering Garbage Collection CSE 668 / ECE 668 Prof. Roger Crawfis

Garbage Collection & Finalizers When the last reference to an object is gone – there are no references to the object remaining – the object is ready to be garbage collected. But we do not know when exactly that will happen. The.Net framework controls garbage collection, so it may be a few seconds or even minutes that the object still lives on in memory. When the garbage collector runs again, it sees that it is a dead object – has no references – calls the finalizer and the object is removed from the heap.

Looking at garbage collection

Suggesting Garbage Collection There is a method that allows you to suggest that garbage collection is run: GC.Collect(); However that is all that it does… suggest. Generally speaking, it is recommended that you do not do this.

Dispose() vs. Finalizers Dispose() runs whenever an object that is created in a using statement is set to null or looses all of it’s references. If you don’t use a using statement, then Dispose() is not called unless you call it directly. Finalizers run automatically when garbage collection occurs.

Finalizers can’t depend on stability

Finalizers and the IDisposable interface Just because you implement IDisposable does not mean that your Dispose() method will get called. If a using statement is not used and the method is not called directly, then it will not run. The finalizer can call Dispose. This is ok, only if the Dispose() method does not depend on other object. The best way to avoid this issue is to always use a using statement when you use an IDisposable object.

Serialization from Dispose()

Tracing Garbage Collectors 1. Initialize three sets for GC cycle bookkeeping Initialize black set to empty Initialize grey set to roots (active variables such as statically allocated and global objects, local variables on stack, variables in the registers) Initialize white set to contain all other objects 2. Repeatedly attempt to detect/mark reachable objects Pick an object from the grey set (until there are no more) Move all the white objects reachable within one reference pointer deep from selected object to the grey set Move the selected object from the grey set to the black set 3. Now reclaim/sweep storage space White set contains left over objects that are NOT reachable

GC Variations on Mark and Sweep Copying Moving/non-moving GC (change/move address of objects in memory) Incremental Interleaved (incremental) or parallel (concurrent) or stop-the-world Conservative Identification of pointers (precise, conservative, partly conservative) Generational Collect in younger/newer generation

Garbage Collection in.NET The CLR has a three generation based system. The theory is: The newer an object is, the shorter its lifetime will be. The older an object is, the longer its lifetime will be. Collecting a portion of the heap is faster than collecting the whole heap. If it existed last time and was not garbage, chances are it is still not garbage.

Garbage Collection in.NET Generation 0 has all of the newly created objects. When it’s buffer is full, garbage collection happens. Anything not garbage is promoted to Generation 1. Tests before 2006 indicated the a generation 0 garbage collection took less than 1 millisecond. Generation 1 and 2 occur along with generation 0 when the CLR determines that their sizes have gotten too large. Special handling of very large memory allocations.