Share via


how to dispose array

Question

Thursday, January 12, 2012 9:29 AM

Hi,

I have this function that should be called multimple times.And i want to make sure that int[,] arrays be disposed / they dont use any memory at the end of the function.

        private byte[,] sobel(byte[,] xms)
        {
            int[,] intpixelX = new int[720, 576];
            int[,] intpixelY = new int[720, 576];
            for (int y = 0; y < 288; y++)
            {
                for (int x = 360; x < 720; x++)
                {
                    int one = xms[x - 1, y - 1] * (-1);
                    int two = xms[x, y - 1] * (-2);
                    int three = xms[x + 1, y - 1] * (-1);
                    int four = xms[x - 1, y] * 0;
                    int five = xms[x, y] * 0;
                    int six = xms[x + 1, y] * 0;
                    int seven = xms[x - 1, y + 1];
                    int eight = xms[x, y + 1] * 2;
                    int nine = xms[x + 1, y + 1];
                    int result = one + two + three + four + five + six + seven + eight + nine;
                    intpixelY[x, y] = result;
                }
            }
            for (int y = 0; y < 288; y++)
            {
                for (int x = 360; x < 720; x++)
                {
                    int one = xms[x - 1, y - 1] * (-1);
                    int two = xms[x, y - 1] * 0;
                    int three = xms[x + 1, y - 1];
                    int four = xms[x - 1, y] * (-2);
                    int five = xms[x, y] * 0;
                    int six = xms[x + 1, y] * 2;
                    int seven = xms[x - 1, y + 1] * (-1);
                    int eight = xms[x, y + 1] * 0;
                    int nine = xms[x + 1, y + 1];
                    int result = one + two + three + four + five + six + seven + eight + nine;
                    intpixelX[x, y] = result;
                }
            }
            for (int y = 0; y < 288; y++)
            {
                for (int x = 360; x < 720; x++)
                {
                    byte btowrite = (byte)Math.Sqrt((intpixelX[x, y] * intpixelX[x, y]) + (intpixelY[x, y] * intpixelY[x, y]));
                    if (btowrite == 255)
                    {
                        xms[x, y] = 1;
                    }
                }
            }
            return xms;
        }
    }

All replies (5)

Thursday, January 12, 2012 9:38 AM âś…Answered

Pirate,

the scope of your int arrays belongs only to this method where you have declared them. So you don`t need to worry.

Hannes

If you have got questions about this, just ask.

In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.

C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/


Thursday, January 12, 2012 9:40 AM | 1 vote

Hi,

"Dispose An Array" is not possible because Dispose is a pattern that is mainly there to handle situations where your class is dealing with unmanaged resources (such as window handles, db connections, sockets, etc.).

The memory allocated by the runtime for the array is better left to the gc to handle.

So call GC.Collect() when you want to clear the Array.

 

Regards,

Ayan Choudhury

If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Thursday, January 12, 2012 9:44 AM

Hi aanch,

It would be great if you pls explain why db connection is considered to be unmanaged code?

Thanks!

Every day its a new learning. Keep Learning!!
If this post answers your question, please click Mark As Answer . If this post is helpful please click Mark as Helpful


Thursday, January 12, 2012 10:24 AM | 1 vote

@Tiya01

Managed resources basically means "managed memory" that is managed by the garbage collector. When you no longer have any references to a managed object (which uses managed memory), the garbage collector will (eventually) release that memory for you.

Unmanaged resources are then everything that the garbage collector does not know about. For example:

  1. Open files
  2. Open network connections
  3. Unmanaged memory
  4. In XNA: vertex buffers, index buffers, textures, etc.

 

Regards,

Ayan Choudhury

 

If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


Friday, May 2, 2014 11:09 AM

Database connections are using OS resources, i.e. TCP/IP, UDP, named pipes and similar.

The .Net connection object is only a wrapper around the native connection, i.e. disposing an SqlConnection object doesn't necessarly mean the underlying native connection is also closed, instead is usually put back into connection pool so that it can be used for another request.

This is why .Net connection objects shouldn't be finalized, only disposed.