• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

URGENT - Variable size issue

Experts,
      Can someone help me figure out this problem please. I have this variable byte[] drive_image = new byte[size] that I am using as part of a test sequence inside of my application. The application tests USB thumbdrives before we ship them out the door to the customer, and I need to figure this out pronto!! [size] is the actual size of the thumbdrive, so as you can imagine it could be 2GB or more. Sorry I can't be more specific about the test details (like why would I want to do this??).
       What I need to know is how can I try to create this variable, and if it throws an out of memory exception, how can I retry with the size/2 until it can find a large enough contiguous block of memory? Any help would be greatly appreciated!

Yeavis
0
Yeavis
Asked:
Yeavis
1 Solution
 
dkloeckCommented:
That should do it:

byte[] createDriveImage(int size)
        {
            byte[] drive_image;
            try
            {
                drive_image = new byte[size];
            }
            catch
            {
                return createDriveImage(size / 2);
            }
            return drive_image;
        }
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Another...

            long size = 2147483648; // 2gb
            byte[] drive_image = null;
            bool success = false;
            while (!success && size > 0)
            {
                try
                {
                    drive_image = new byte[size];
                    success = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    if (size >= 2)
                    {
                        size = size / 2;
                    }
                    else
                    {
                        size = 0;
                    }
                }
            }

            // now do something with "drive_image"...
            if (size == 0)
            {
                MessageBox.Show("Unable to allocate an Array");
            }
            else
            {
                MessageBox.Show("Array Size: " + drive_image.Length + " bytes");
            }            
0
 
ShazbotOKCommented:
This here will write until it can write no more and then return the total write size:

        private string testWrite(string filePath,bool cleanItUp)
        {
            System.IO.FileStream fs = System.IO.File.OpenWrite(filePath)
            long iLen = 0;
            while(fs.CanWrite)
            {
                fs.WriteByte(new byte());
                iLen++;
            }
            fs.Close();
            if(cleanItUp)
                System.IO.File.Delete(filePath);

            return "Total Write Size: " + iLen.ToString();
        }
0
 
Bill-HansonCommented:
URGENT, huh?
0
 
YeavisAuthor Commented:
Yeah, it is urgent. Sorry, I have been implementing this and testing to see if I have any problems. So far so good... I really appreciate everyone's input.

Yeavis
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now