In which cases return the System Exception error.Outofmemoryexception

Asked

Viewed 3,980 times

7

Which are the most frequent cases that can return this Exception?

1 answer

5


It’s basically a lack of memory. It occurs more on 32-bit machines that can have applications of up to 2GB in total, in 64-bit it is less common but also has limit, and it is usually very slow before this happens.

It may be general lack of memory or it may be that an object is too large. Usually objects cannot be more than 2GB, save special 64 bit configuration.

Other memory manager problems can cause something like this.

It turns out that this often runs by programmer error. It is not that he is carrying many objects and large in memory, it is that he is not releasing them.

A lot of people think that because . NET has a garbage collector can make any mess and everything is fine. It’s not like that. It cannot hold memory unintentionally. Memory is only released when the . NET knows that there are no references to that portion of memory and it is not an unmanaged resource. If you do this there is a memory leak (it’s Java, but it’s almost the same).

If you are using a class with the interface Disposable and is not closing the appeal, probably with using will leak.

If you are using an event and do not release the subscription list when the object is no longer needed, you will get leaked.

If you put it in a static area or something with a lifespan close to the lifespan of the application then there’s a reference to something you might not need anymore and there’s a leak. This may be true for poorly thought out algorithms.

Have a number of cases described in link above they cause as well, but these are more common.

  • 2

    To top it off, just out of curiosity... There are many cases where you need to treat a well defined information sequentially (for example, read the response of a webservice or read a large text file). However, the answer provided may be too big. Instead of loading everything in memory to then process, you can do the partial processing of what was read and discard as you no longer need the information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.