Performance Monitor is a tool that is native to all variants of the Windows OS. Think of it as Task Manager on steroids. Just do a search for "perfmon", and you'll see it in the start window.
Using Performance Monitor
In the tree view on the left side of the screen click on "Performance Monitor" inside the "Monitoring Tools" folder. Here you can see real time data collection. By default it shows processing percentage. Right click this counter and click "Remove All Counters". Now, right click and select "Add Counters". Expand the "Process" category in the list box on the left, and choose "Private Bytes" Private Bytes is the amount of memory a process has allocated to it exclusively. In the second list box, select the process that represents the IIS worker process for the web application you are monitoring. The process is normally named w3wp. Keep in mind that there is a separate worker process for each application pool. Application pools can be shared among more that just one web application so you need to dig around in IIS to determine what you are looking for. If more that one app pool is running, you will see multiple w3wp instances. You can deduce which worker process is the one you want by comparing the memory usage in peformance monitor with the memory usage in IIS (select the server, then choose worker processes). I usually just end up selecting them all. Select "Add" and then "OK". Now you'll see the memory usage scrolling across the screen. However, since the usage is in bytes, and the y axis only goes up to 100, the line is just a straight line along the top of the graph. So, you'll need to scale down the numbers by right clicking each counter, and choosing "Properties". I have found a scale of 0.0000001 works good for this.
This only shows you data from the last minute or so. If you want to record usage from a longer period of time, you need to set up a "Data Collector Set". Expand "Data Collector Sets" in the tree view and right click the "User Defined" folder. Choose "New" and then "Data Collector Set" from the menu. Choose a basic template. Click on your new data collector set, and then double click on Performance Counter. Remove the counter that's there, and add the counters like you did above. Then set the sample interval. Right click the collector set and click properties and select the stop condition tab. Here you can set how long you want the collector to run. After setting all of this up, you can right click the collector and choose start. When it completes there will be a report in the reports folder.
Monday, September 23, 2013
Thursday, September 19, 2013
.net Memory Leak?
It was suggested by a colleague that one of our web applications may have a "memory leak". An application is said to have a memory leak when it uses memory but doesn't release the memory when it is done with it. Over time, the server the application is running on will run out of memory.
With .net, (most of the time) you don't need to worry about releasing memory. Most people don't understand how and when to do it anyway. You create an object which takes up memory. The object then goes out of scope and at some indeterminate time in the future, the "garbage collector" process (part of the .net runtime) will clean up and release this memory for you.
The garbage collector will only clean up .net objects. These are called "managed resources". Sometimes our .net classes contain references to objects that are not .net. These are called "unmanaged resources". .net classes that contain references to unmanaged resources are supposed to implement the IDisposable interface. This exposes a Dispose method that will free up the unmanaged resource. You should assume that if you are instantiating an object of a class that implements IDisposable, you should call the Dispose method or declare the object with the Using keyword. The Using keyword is preferred since it will call the Dispose method even if an exception occurs while using the object. If you fail to do this, you are taking a risk of the unmanaged resource sticking around in memory forever. If you create a class that contains an instance of a class that implements IDisposable, then your class should also implement IDisposable and call the Dispose method from your Dispose method. If you follow these rules, you simply shouldn't have memory leaks.
How do we determine if there is a memory leak? The only way is to monitor memory usage of the application. With web applications, this presents challenges. Web applications can use what appears to be a lot of memory. However, this does not necessarily indicate a problem. Especially if the web application is under a heavy load. The web application will use what it needs as more users make requests. Web applications run inside a worker process for the application pool that is configured for the web site in IIS. The process won't give up the memory unless it feels "pressure" to do so by some other process. Reallocating memory takes processing power, and IIS simply won't do it until it feels it needs to. As you can see, there are going to be some fluctuations in the amount of memory that an application uses that are not related at all to a problem or a "memory leak". Another thing to keep in mind is that IIS by default will recycle each app pool approximately once a day. Recycling the app pool essentially wipes out all the memory and starts fresh. So, if the app pool is configured to recycle more often than that, you may want to adjust that so you can get a good feel of memory usage over a longer period of time. So, to determine if there is a memory leak, you cannot rely on memory usage observations in the short term. You must monitor the memory usage over a long period (a day or two). If the memory usage gradually increases throughout the period, you probably have a problem. If the increase is correlated to an increase in load, it may just be normal.
With .net, (most of the time) you don't need to worry about releasing memory. Most people don't understand how and when to do it anyway. You create an object which takes up memory. The object then goes out of scope and at some indeterminate time in the future, the "garbage collector" process (part of the .net runtime) will clean up and release this memory for you.
The garbage collector will only clean up .net objects. These are called "managed resources". Sometimes our .net classes contain references to objects that are not .net. These are called "unmanaged resources". .net classes that contain references to unmanaged resources are supposed to implement the IDisposable interface. This exposes a Dispose method that will free up the unmanaged resource. You should assume that if you are instantiating an object of a class that implements IDisposable, you should call the Dispose method or declare the object with the Using keyword. The Using keyword is preferred since it will call the Dispose method even if an exception occurs while using the object. If you fail to do this, you are taking a risk of the unmanaged resource sticking around in memory forever. If you create a class that contains an instance of a class that implements IDisposable, then your class should also implement IDisposable and call the Dispose method from your Dispose method. If you follow these rules, you simply shouldn't have memory leaks.
How do we determine if there is a memory leak? The only way is to monitor memory usage of the application. With web applications, this presents challenges. Web applications can use what appears to be a lot of memory. However, this does not necessarily indicate a problem. Especially if the web application is under a heavy load. The web application will use what it needs as more users make requests. Web applications run inside a worker process for the application pool that is configured for the web site in IIS. The process won't give up the memory unless it feels "pressure" to do so by some other process. Reallocating memory takes processing power, and IIS simply won't do it until it feels it needs to. As you can see, there are going to be some fluctuations in the amount of memory that an application uses that are not related at all to a problem or a "memory leak". Another thing to keep in mind is that IIS by default will recycle each app pool approximately once a day. Recycling the app pool essentially wipes out all the memory and starts fresh. So, if the app pool is configured to recycle more often than that, you may want to adjust that so you can get a good feel of memory usage over a longer period of time. So, to determine if there is a memory leak, you cannot rely on memory usage observations in the short term. You must monitor the memory usage over a long period (a day or two). If the memory usage gradually increases throughout the period, you probably have a problem. If the increase is correlated to an increase in load, it may just be normal.
Friday, September 6, 2013
Add an InstallShield LE project for a Windows Service
First you must have a windows service project with a ProjectInstaller class added to it.
Then just add a new project and select the InstallShield Limited Edition Project template. Walk through the wizard (Project Assistant) and just add the project output from your windows service project. You do this on the "Application Files" step. Just click the Add Project Outputs button in the lower right hand corner. Click the Primary Output check box as shown below:
Then, in Solution Explorer, double-click on "Files" which is under "Specify Application Data". Right-click your project output, and choose properties. You'll see this. Just check that Installer Class checkbox, click OK, and you're good to go.
Then just add a new project and select the InstallShield Limited Edition Project template. Walk through the wizard (Project Assistant) and just add the project output from your windows service project. You do this on the "Application Files" step. Just click the Add Project Outputs button in the lower right hand corner. Click the Primary Output check box as shown below:
Then, in Solution Explorer, double-click on "Files" which is under "Specify Application Data". Right-click your project output, and choose properties. You'll see this. Just check that Installer Class checkbox, click OK, and you're good to go.
Visual Studio 2012 and Setup Projects
Out of the box setup and deployment projects no longer exist in Visual Studio with the release of VS 2012. However, Microsoft officially recommends using a product called InstallShield Limited Edition for Visual Studio which can be downloaded here. This installs a project template in Visual Studio called InstallShield Limited Edition Project. There is no official documentation for how to use and configure the product. You have to rely on other blog posts and a few videos. From what I can tell, it works great for windows applications. For a web application, I would not recommend using an installer. You have to jump through a lot of hoops to configure the installer for this type of application, and it is so much easier to simply have your network admin set up a virtual directory, and copy the files from a location where you have published your files.
Wednesday, August 14, 2013
Lambda Expressions
A function can be sent as a parameter to another sub procedure or function.
For example:
The selector parameter is defined with the Func type. This means that a function must be passed in with a return type of boolean and takes a parameter of type Book.
So when we call the Filter extension method, can do the following:
Let's say you have a function that meets the criteria for the parameter:
You could send this function as a parameter like this:
Or you could use a lambda expression and seriously streamline your code like this:
So, "x" represents the Book input parameter.
For example:
<Extension>
Public Iterator Function Filter(aBookEnum As IEnumerable(Of Book), selector As Func(Of Book, Boolean)) As IEnumerable(Of Book)
For Each thisBook As Book In aBookEnum
If selector(thisBook) Then
Yield thisBook
End If
Next
End Function
The selector parameter is defined with the Func type. This means that a function must be passed in with a return type of boolean and takes a parameter of type Book.
So when we call the Filter extension method, can do the following:
Let's say you have a function that meets the criteria for the parameter:
Function GetBookOne(myBook As Book) As Boolean
Return myBook.Title = "Book One"
End Function
You could send this function as a parameter like this:
Dim MySelector As Func(Of Book, Boolean) = AddressOf GetBookOne MyBookShelf.Filter(MySelector)
Or you could use a lambda expression and seriously streamline your code like this:
MyBookShelf.Filter(Function(x) x.Title = "Book One")
So, "x" represents the Book input parameter.
Tuesday, August 13, 2013
What is IEnumerable(Of T)?
IEnumerable is the base interface for all generic collections that can be enumerated (iterated through). Examples of classes that implement IEnumerable include Lists, Arrays, Stacks, and Queues. You can also write your own classes that implement it. Any class that implements this interface can have LINQ queries performed on its enumerator. You can also iterate through the collection using the For Each operator. T is the type of item that the collection stores. When you perform LINQ queries, you often get anonymous classes that implement the IEnumerable interface in return.
The System.Linq namespace contains all kinds of extension methods that operate on classes that implement the IEnumerable interface. This is what allows you to write LINQ queries against these objects.
The System.Linq namespace contains all kinds of extension methods that operate on classes that implement the IEnumerable interface. This is what allows you to write LINQ queries against these objects.
Use Syntax Highlighter to display code in your Blogger posts
Edit the HTML of your blogger template. From your blog dashboard, click Template on the left side, and then click the Edit HTML button. Add the following to the head section.
This example is set up for JavaScript, XML, and VB, but you can find other "brushes" here:
http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/
Switching to HTML when writing your post, if you wanted to demonstrate a bit of HTML, you do this:
Note that I had to escape the angle brackets with the < and >.
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeEclipse.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
<script language='javascript' type='text/javascript'>
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.all();
</script>
This example is set up for JavaScript, XML, and VB, but you can find other "brushes" here:
http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/
Switching to HTML when writing your post, if you wanted to demonstrate a bit of HTML, you do this:
<pre class="brush:xml;">
<script src="StevesJScript.js" type="text/javascript"></script>
</pre>
Note that I had to escape the angle brackets with the < and >.
Subscribe to:
Posts (Atom)
