# OutOfMemoryException

***

**1. Allocating a Large Array**

```csharp
int[] largeArray = new int[Int32.MaxValue]; // throws OutOfMemoryException
```

**2. Creating a Large Byte Array**

```csharp
byte[] largeByteArray = new byte[Int32.MaxValue]; // throws OutOfMemoryException
```

**3. Using a Large Object Heap**

```csharp
byte[] largeObject = new byte[Int32.MaxValue]; // throws OutOfMemoryException if not using Large Object Heap
```

**4. Using a MemoryStream with Large Capacity**

```csharp
using (MemoryStream largeMemoryStream = new MemoryStream())
{
    // Fill the stream with a large amount of data
    largeMemoryStream.Write(new byte[Int32.MaxValue], 0, Int32.MaxValue);
} // throws OutOfMemoryException if not enough memory available
```

**5. Loading a Large Image into Memory**

```csharp
try
{
    Image largeImage = Image.FromFile("large-image.png"); // throws OutOfMemoryException if image is too large
}
catch (OutOfMemoryException ex)
{
    // Handle the exception
}
```

**6. Using a Large Collection of Objects**

```csharp
List<object> largeCollection = new List<object>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeCollection.Add(new object()); // throws OutOfMemoryException if too many objects are created
}
```

**7. Loading a Large XML Document into Memory**

```csharp
try
{
    XmlDocument largeXmlDocument = new XmlDocument();
    largeXmlDocument.LoadXml("large-xml.xml"); // throws OutOfMemoryException if XML document is too large
}
catch (OutOfMemoryException ex)
{
    // Handle the exception
}
```

**8. Using a Large Dictionary**

```csharp
Dictionary<string, string> largeDictionary = new Dictionary<string, string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeDictionary.Add($"key-{i}", $"value-{i}"); // throws OutOfMemoryException if too many key-value pairs are added
}
```

**9. Using a Large Queue**

```csharp
Queue<string> largeQueue = new Queue<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeQueue.Enqueue($"item-{i}"); // throws OutOfMemoryException if too many items are enqueued
}
```

**10. Using a Large Stack**

```csharp
Stack<string> largeStack = new Stack<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeStack.Push($"item-{i}"); // throws OutOfMemoryException if too many items are pushed
}
```

**11. Using a Large HashSet**

```csharp
HashSet<string> largeHashSet = new HashSet<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeHashSet.Add($"item-{i}"); // throws OutOfMemoryException if too many items are added
}
```

**12. Using a Large SortedSet**

```csharp
SortedSet<string> largeSortedSet = new SortedSet<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeSortedSet.Add($"item-{i}"); // throws OutOfMemoryException if too many items are added
}
```

**13. Using a Large ConcurrentDictionary**

```csharp
ConcurrentDictionary<string, string> largeConcurrentDictionary = new ConcurrentDictionary<string, string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentDictionary.TryAdd($"key-{i}", $"value-{i}"); // throws OutOfMemoryException if too many key-value pairs are added
}
```

**14. Using a Large ConcurrentQueue**

```csharp
ConcurrentQueue<string> largeConcurrentQueue = new ConcurrentQueue<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentQueue.Enqueue($"item-{i}"); // throws OutOfMemoryException if too many items are enqueued
}
```

**15. Using a Large ConcurrentStack**

```csharp
ConcurrentStack<string> largeConcurrentStack = new ConcurrentStack<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentStack.Push($"item-{i}"); // throws OutOfMemoryException if too many items are pushed
}
```

**16. Using a Large BlockingCollection**

```csharp
BlockingCollection<string> largeBlockingCollection = new BlockingCollection<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeBlockingCollection.Add($"item-{i}"); // throws OutOfMemoryException if too many items are added
}
```

**17. Using a Large ConcurrentBag**

```csharp
ConcurrentBag<string> largeConcurrentBag = new ConcurrentBag<string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentBag.Add($"item-{i}"); // throws OutOfMemoryException if too many items are added
}
```

**18. Using a Large List**

```csharp
List<int> largeList = new List<int>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeList.Add(i); // throws OutOfMemoryException if too many items are added
}
```

**19. Using a Large Dictionary\<TKey, TValue>**

```csharp
Dictionary<int, string> largeDictionary = new Dictionary<int, string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeDictionary.Add(i, $"value-{i}"); // throws OutOfMemoryException if too many items are added
}
```

**20. Using a Large HashSet**

```csharp
HashSet<int> largeHashSet = new HashSet<int>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeHashSet.Add(i); // throws OutOfMemoryException if too many items are added
}
```

**21. Using a Large SortedSet**

```csharp
SortedSet<int> largeSortedSet = new SortedSet<int>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeSortedSet.Add(i); // throws OutOfMemoryException if too many items are added
}
```

**22. Using a Large ConcurrentDictionary\<TKey, TValue>**

```csharp
ConcurrentDictionary<int, string> largeConcurrentDictionary = new ConcurrentDictionary<int, string>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentDictionary.TryAdd(i, $"value-{i}"); // throws OutOfMemoryException if too many items are added
}
```

**23. Using a Large ConcurrentQueue**

```csharp
ConcurrentQueue<int> largeConcurrentQueue = new ConcurrentQueue<int>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentQueue.Enqueue(i); // throws OutOfMemoryException if too many items are added
}
```

**24. Using a Large ConcurrentStack**

```csharp
ConcurrentStack<int> largeConcurrentStack = new ConcurrentStack<int>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentStack.Push(i); // throws OutOfMemoryException if too many items are pushed
}
```

**25. Using a Large BlockingCollection**

```csharp
BlockingCollection<int> largeBlockingCollection = new BlockingCollection<int>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeBlockingCollection.Add(i); // throws OutOfMemoryException if too many items are added
}
```

**26. Using a Large ConcurrentBag**

```csharp
ConcurrentBag<int> largeConcurrentBag = new ConcurrentBag<int>();
for (int i = 0; i < Int32.MaxValue; i++)
{
    largeConcurrentBag.Add(i); // throws OutOfMemoryException if too many items are added
}
```

**27. Using a Large Array of Structs**

```csharp
struct MyStruct
{
    public int Value;
}

MyStruct[] largeArrayOfStructs = new MyStruct[Int32.MaxValue]; // throws OutOfMemoryException
```

**28. Using a Large Array of Classes**

```csharp
class MyClass
{
    public int Value;
}

MyClass[] largeArrayOfClasses = new MyClass[Int32.MaxValue]; // throws OutOfMemoryException
```

**29. Using a Large Array of Delegates**

```csharp
delegate void MyDelegate();

MyDelegate[] largeArrayOfDelegates = new MyDelegate[Int32.MaxValue]; // throws OutOfMemoryException
```

**30. Using a Large Array of Events**

```csharp
event EventHandler MyEvent;

MyEvent += HandleMyEvent;

// Add a large number of event handlers
for (int i = 0; i < Int32.MaxValue; i++)
{
    MyEvent += HandleMyEvent;
} // throws OutOfMemoryException
```

**31. Using a Large Array of Threads**

```csharp
Thread[] largeArrayOfThreads = new Thread[Int32.MaxValue]; // throws OutOfMemoryException
```

**32. Using a Large Array of Tasks**

```csharp
Task[] largeArrayOfTasks = new Task[Int32.MaxValue]; // throws OutOfMemoryException
```

**33. Using a Large Array of Semaphores**

```csharp
Semaphore[] largeArrayOfSemaphores = new Semaphore[Int32.MaxValue]; // throws OutOfMemoryException
```

**34. Using a Large Array of Mutexes**

```csharp
Mutex[] largeArrayOfMutexes = new Mutex[Int32.MaxValue]; // throws OutOfMemoryException
```

**35. Using a Large Array of Locks**

```csharp
lock[] largeArrayOfLocks = new lock[Int32.MaxValue]; // throws OutOfMemoryException
```

**36. Using a Large Array of ReadWriteLocks**

```csharp


```
