Archive

Posts Tagged ‘GC’

When and how to use GC.KeepAlive in .Net

August 27th, 2011 1 comment

Let’s look on the following C# code example (ignore the correctness of the code and its logic)

    /// <summary>
    /// Indicates any service Logger for example
    /// </summary>
    class MyService
    {
        public MyService(string fileName)
        {
            //Do some initializations
        }

        public static void DoSomething()
        {
            //some calculations, or logs creation
        }
    }
    //*************************************************
    /// <summary>
    /// Usage example class 
    /// </summary>
    class MyLogic
    {
        public void Foo()
        {
            //cals static method of the initialized service
            MyService.DoSomething();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //Create our service 
            MyService myService = new MyService("temp.txt");
            //Wait here for user
            Console.ReadLine();
        }
    }

Read more…

Categories: .Net, GC Tags: ,

Getting OutOfMemory exception in .Net process, possible solutions.

May 7th, 2011 No comments

Getting OutOfMemoryexception in .Net process, possible solutions.

I want to share with you very interesting and complicated problem that we managed to overcome. I will describe all our resolution procedure step by step. Hope it will help others as it helped us, in any case I have learned a lot.

 

We have encountered a serious problem such as OutOfMemoryException

In my application – WinForm application i have managed code and un-managed (C/C++) 3’rd party code that perform many un-managed memory allocations at beginning of the process load and during runtime.

As we started to suffer from memory problems, we decided to enlarge address space that our process can use from 2 GB to 3 GB. This operation was done by using /LARGEADDRESSAWARE flag for our process and OS configured with /3GB. Unfortunately enlarge address space to our application seems to be good solution, but it didn’t help in our case.

So we started debugging and deeply investigate our Outofmemory problems. As usual main tool that we have used was WinDbg. Interesting things were discovered by using WinDbg and using following command “!address –summary” which shows us that we have not reached 3GB memory limit. Although that we have free memory, the memory wasn’t continuous so the CLR or native code failed to allocate memory. The problem appear to be defragmentation problem.

Read more…

Categories: .Net, C# Tags: , , , , ,