Some Useful .NET Techniques Blair Schneider McKay Blair Schneider McKay Clareos, Inc. Clareos, Inc. http://home.comcast.net/ http://home.comcast.net/ ~blairschneidermckay/ ~blairschneidermckay/ email: [email protected]Presented to Upsilon Pi Epsilon Presented to Upsilon Pi Epsilon at The University of Michigan at The University of Michigan – Dearborn – Dearborn November 10 November 10 th th , 2004 , 2004
33
Embed
Some Useful.NET Techniques Blair Schneider McKay Clareos, Inc. blairschneidermckay/ email: [email protected] Presented.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
. . . and, of course, “memory”.. . . and, of course, “memory”.
Blair Schneider McKay
.NET techniques – Slide 5 Nov. 10, 2004
Agenda
Safe Resource ManagementSafe Resource Management• Leaks, Locks, and Garbage CollectionLeaks, Locks, and Garbage Collection• Exception SafetyException Safety• Dispose/DestructDispose/Destruct
XML SerializationXML Serialization• .NET Facilities.NET Facilities• Uses of SerializationUses of Serialization
What To Do About OutsourcingWhat To Do About Outsourcing
Blair Schneider McKay
.NET techniques – Slide 6 Nov. 10, 2004
What is a Leak?
Many operating systems automatically reclaim Many operating systems automatically reclaim resources when an application terminates, but…resources when an application terminates, but…
• Code which depends on this is Code which depends on this is less portableless portable..• Leaks hamper performance and cause crashes in Leaks hamper performance and cause crashes in
all but very simple and short-lived apps.all but very simple and short-lived apps.• It’s embarassing.It’s embarassing.
When an application loses all references to a When an application loses all references to a resource and thus can no longer clean it up.resource and thus can no longer clean it up.
Blair Schneider McKay
.NET techniques – Slide 7 Nov. 10, 2004
ANSI C codeANSI C code::
void void leaky_converter(leaky_converter(unsigned int unsigned int input)input){{
Widget* ret =Widget* ret = new new Widget( );Widget( );preProcessWidget ( ret );preProcessWidget ( ret );return return ret;ret;
}}
Blair Schneider McKay
.NET techniques – Slide 9 Nov. 10, 2004
What Is a Lock?
classclass Lock Lock
{{
public:public:
Lock( Resource& r ) : r_( r ) { acquire ( r ); }Lock( Resource& r ) : r_( r ) { acquire ( r ); }
~Lock() { free( r_ ); }~Lock() { free( r_ ); }
private:private:
Resource& r_;Resource& r_;
};};
Blair Schneider McKay
.NET techniques – Slide 10 Nov. 10, 2004
Demo – Using Locks
UPE4_lock0UPE4_lock0
Blair Schneider McKay
.NET techniques – Slide 11 Nov. 10, 2004
Garbage Collection
Fundamental part of .NET framework.Fundamental part of .NET framework. ““Managed Code” == Garbage Collection.Managed Code” == Garbage Collection. Critical for “always on” or long-run apps.Critical for “always on” or long-run apps.
C# codeC# code::
void void safe_function( )safe_function( ){{
Widget w =Widget w = new new Widget( );Widget( );doSomething( w );doSomething( w );returnreturn;;
}}
Blair Schneider McKay
.NET techniques – Slide 12 Nov. 10, 2004
When GC Isn’t Sufficient
You don’t know when GC will happen. You don’t know when GC will happen. (“Non-deterministic termination.”)(“Non-deterministic termination.”)
You sometimes can force GC to run, but You sometimes can force GC to run, but that’s incredibly wasteful.that’s incredibly wasteful.
GC is typically designed as a “one-size-fits-GC is typically designed as a “one-size-fits-all” system, and it often biased towards all” system, and it often biased towards memory management.memory management.
Remember those “other” resources?Remember those “other” resources?
Blair Schneider McKay
.NET techniques – Slide 13 Nov. 10, 2004
Demo – Ill-behaved Stream Classes
FileGen0FileGen0
Blair Schneider McKay
.NET techniques – Slide 14 Nov. 10, 2004
Agenda
Safe Resource ManagementSafe Resource Management• Leaks, Locks, and Garbage CollectionLeaks, Locks, and Garbage Collection• Exception SafetyException Safety• Dispose/DestructDispose/Destruct
XML SerializationXML Serialization• .NET Facilities.NET Facilities• Uses of SerializationUses of Serialization
What To Do About OutsourcingWhat To Do About Outsourcing
Safe Resource ManagementSafe Resource Management• Leaks, Locks, and Garbage CollectionLeaks, Locks, and Garbage Collection• Exception SafetyException Safety• Dispose/DestructDispose/Destruct
XML SerializationXML Serialization• .NET Facilities.NET Facilities• Uses of SerializationUses of Serialization
What To Do About OutsourcingWhat To Do About Outsourcing
Blair Schneider McKay
.NET techniques – Slide 26 Nov. 10, 2004
Uses of Serialization Save program state for later recall.Save program state for later recall. Parameterize programs.Parameterize programs. Interprocess communication. (Why not?)Interprocess communication. (Why not?) Exchange data across machine, architecture, Exchange data across machine, architecture,
programming language, etc.programming language, etc. Send data and/or objects across networks Send data and/or objects across networks
Safe Resource ManagementSafe Resource Management• Leaks, Locks, and Garbage CollectionLeaks, Locks, and Garbage Collection• Exception SafetyException Safety• Dispose/DestructDispose/Destruct
XML SerializationXML Serialization• .NET Facilities.NET Facilities• Uses of SerializationUses of Serialization
What To Do About OutsourcingWhat To Do About Outsourcing
Blair Schneider McKay
.NET techniques – Slide 29 Nov. 10, 2004
What to Do About Outsourcing – 1
Be good at what you do!Be good at what you do! Study. Study.
(Sometimes you just have to RTFM.)(Sometimes you just have to RTFM.) Practice.Practice.
(Sometimes you just need to mess around.)(Sometimes you just need to mess around.) Do what the gurus do.Do what the gurus do.
(design patterns, best practices, idioms, etc.)(design patterns, best practices, idioms, etc.)
Or, What to Do About a Soft Job MarketOr, What to Do About a Soft Job Market
Blair Schneider McKay
.NET techniques – Slide 30 Nov. 10, 2004
What to Do About Outsourcing – 2
Or, What to Do About a Soft Job MarketOr, What to Do About a Soft Job Market
Learn to Communicate Well.Learn to Communicate Well. If you need, take your time and write your thoughts down.If you need, take your time and write your thoughts down. Ask a co-worker to review your written work.Ask a co-worker to review your written work. Non-technical bosses often have no independent way to Non-technical bosses often have no independent way to
evaluate your work. The quality of your communication evaluate your work. The quality of your communication with them will matter a lot. with them will matter a lot.
History demonstrates that if you can learn to explain History demonstrates that if you can learn to explain complex issues in simple language, you will Go Far.complex issues in simple language, you will Go Far.
Blair Schneider McKay
.NET techniques – Slide 31 Nov. 10, 2004
What to Do About Outsourcing – 3
Or, What to Do About a Soft Job MarketOr, What to Do About a Soft Job Market
Don’t “hide your light.”Don’t “hide your light.” ““It ain’t bragging if you done it.”It ain’t bragging if you done it.”
(Of course, this can easily go too far.)(Of course, this can easily go too far.) The The bestbest way to ensure that you get fair credit for way to ensure that you get fair credit for
your work is to promote your work your work is to promote your work as part ofas part of a a team effort. team effort.
The more generous we are with each other, the The more generous we are with each other, the more successful we all will be.more successful we all will be.
Blair Schneider McKay
.NET techniques – Slide 32 Nov. 10, 2004
What to Do About Outsourcing - 4
Above all, don’t despair!Above all, don’t despair! People don’t like to work with unhappy co-workers. People don’t like to work with unhappy co-workers.
Bosses don’t like to hire or to promote them.Bosses don’t like to hire or to promote them. This does This does notnot mean that you must smile and just “take it” mean that you must smile and just “take it”
if people or events make you miserable, nor that you if people or events make you miserable, nor that you shouldn’t strive to improve things.shouldn’t strive to improve things.
Bosses Bosses willwill listen to problems and concerns if they are listen to problems and concerns if they are presented calmly and (most especially) if you offer them presented calmly and (most especially) if you offer them solutionssolutions, no matter how tentative or incomplete., no matter how tentative or incomplete.
Or, What to Do About a Soft Job MarketOr, What to Do About a Soft Job Market