Saturday, February 25, 2012

Releasing Memory used in Windows Form Application Using C#


How to release memory used by an application - C#


Hi friends this is very common problem which we face while doing operation with very large objects



Some time we need to do operation with huge amount of binary data or some custom graphics drawing to do the needful things in our application.
If we don’t have sufficient memory to run our application we need to dispose unused objects from our application.
We usually write finally block in our program to release unused object or indirectly calls Garbage collector but it does not release the actually memory.

Usually in windows application memory is associated with process. If you kill you process your memory get released. But suppose we are doing operation like uploading bulk documents to database which has size around 2 to 3 GB then it fails to upload document to database, because each time binary object is created to convert physical document in to binary form to write in database.
This Issue is solved by Flushing memory by doing a bit of code:
Lets see how to do it.:
First you need to include following Two namespaces:
Using Microsoft.Win32;
Using System.Runtime.InteropServices;
It’s up to you to create a separate code file or include the Flush method in the same class where you are dong memory consumption operation:
My advice is to create a separate class so that you can use it anywhere in your application if you mostly do such type of operation.
Add following class in your solution: 
public class ReleaseMemory
{
[DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =
CharSet.Ansi, SetLastError = true)]

private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int
maximumWorkingSetSize);

public static void FlushMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
Next call FlushMemory() method from you class to dispose or release unused memory
Eg: ReleaseMemory.FlushMemory();
It is a static method so don’t need any object of ReleaseMemory() class.

Feel free to write you suggestion 
Happy coding
Best Regards
Gireesh Painuly

Saturday, February 11, 2012

Creating Installer and uninstaller in Visual Studio 2008/2010.


Creating Installer In Visual Studio 2008/2010
Hi Friends, it's very common requirements some time to create installer and uninstaller in .net windows application. I have give some ideas how to perform both of these operation in a single application.
Setp:-1. Right click on Solution - =>Add => New Proejct
setp-2 Select Other Project Types. Select SetupProject from VS Installed Templetes.
Setp-3 Give Proper name for Setup.
Setp-4 Filse System (Setup Window Opens)
setp-5 Select Application Folder in left panel and right click on right panel.
setp-6 Add-> Project Output-> Primary Output, click on Ok button
setp-7 Select user's Desktop in left panel and right click on right panel.
setp-8 Select Create Shortcut-Application Folder -> Primary output.
setp-9 Rename current desktop shortcut.Change Icon from Property.
setp-10 Do same things for user's Programs Menu.
setp-11 Finaly rebuild you setup project.
setp-12 Find you setup in relese folder of setup project.


Creating UnInstaller: ( In Visual Studio 2008/2010)

To create UnInsteller we need to do following Things:
Setp-1: First Create Insteller
setp-2. In the Setup Project –> File System windows –> Right Click “File System on Target machine” –> add a Special Folder, select System Folder;
Setp-3: Into this system folder Add a file. Browse for msiexec.exe from local System32 folder and add it. Override default properties of this file as follows:

Condition:=Not Installed (make sure you put ‘Not Installed’ exactly like that, same case and everything),
Permanent:=True,
System:=True,
Transitive:=True,
Vital:=False.
Setp-4: ) Create a new shortcut under the ‘Users Program Menu’, Set Target to the System Folder which you created in the Step-3
and point it’s at the msiexec.exe. Rename the shortcut to ‘Uninstall Your Application’. Set the Arguments property to /x{space}[ProductCode].
       Note: To get Product code: select you setup Project and check in Property will display Product code, copy it including {} and palce it after /X <Space> <Product code>
Step-5:Build the project, ignore warning about the fact that msiexec should be excluded, DONT exclude it or the setup project wont build.

The ‘Not Installed’ condition and Permananet:=True ensure that the msiexec.exe is only placed into the system folder as part of the install IF it doesn’t aready exist, and it is not removed on an uninstall - therefore it;s pretty safe to ignore that warning and just go for it.

That's it.. Good luck..