The following script (which must be included in a .aspx file):
void Page_Load(object sender, System.EventArgs e) {
int count = 2000;
int size = 1000000;
for(int n=0; n < count; n++) {
string key = "cache_test_" + n;
object obj = new byte[size];
Cache[key] = obj;
}
}
will cause OutOfMemory exceptions on .Net 1.0, 1.1 and 1.1 SP1
Note that even if a Thread.Sleep(10) statement is embedded in the loop,
exceptions will still be thrown.
It seems as if the Cache object does not check for available memory, when
accepting new items.
Another way to force OutOfMemory exceptions is to fill the Cache to within a
fraction of the available memory. After that, exceptions will be thrown when
trying to allocate ordinary objects. Ie.:
1) Put 600MBs worth of objects into the Cache.
2) Allocate some objects, as in:
object obj1 = new byte[1000000];
object obj2 = new byte[1000000];
object obj3 = new byte[1000000];
object obj4 = new byte[1000000];
object obj5 = new byte[1000000];
object obj6 = new byte[1000000];
object obj7 = new byte[1000000];
object obj8 = new byte[1000000];
The numbers may vary, but it is very easy to force OutOfMemory's this way.
Has anyone had similar problems, and do anyone have a solution?
Best,
Ole
Alvin Bruney [MVP] - 23 Sep 2004 19:34 GMT
i've been able to reproduce similar behavior on another discussion. my take
at the time was that the gc was getting overwhelmed. i don't have a work
around at this time.

Signature
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
> The following script (which must be included in a .aspx file):
>
[quoted text clipped - 44 lines]
>
> Ole
Gabe Garza - 24 Sep 2004 13:53 GMT
Ole,
The problem is that the previous values of 'obj' are not unreachable, so GC
thinks you still want access to the previous values of 'obj' so it's not
going to free that memory. That's why your getting an OutOfMemory Exception,
all your previous 'obj' ojects are accumulating in memory. Not too mention
that
you have the cache filling up as well.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/d
otnetperftechs.asp
Follow the above link, look at Contents and click on Garbage Collection.
If you make 'obj' null this now makes 'obj' unreachable. I know in Java the
GC only runs once there's no activity for a pre-determine amount of time.
I'm pretty sure GC in .NET is not going to get called every 10 milliseconds
or less, so you should just call GC.Collect() after you set 'obj' to null.
Cache[key] = obj;
obj = null;
GC.Collect();
This does take care of your OutOfMemory Exception but now it brings you to a
new problem.
aspnet_wp.exe (PID: 1880) was recycled because memory consumption
exceeded the 306 MB (60 percent of available RAM).
This process is comsuming way too much memory. On my laptop I have 512 megs
of RAM. So my 60 percent will probably differ from everyone else's.
Without knowing why you need to allocate so much memory and put it in cache,
I would think you'd probably want to think about how to accomplish what you
want without allocating so much RAM. Maybe having Cache[key] contain a
pointer to
a file instead, then fill the contents of the file with what you would have
filled 'obj' with.
I know that the sample you gave was just to demostrate how to get the
OutOfMemory Exception, but I think you can see now that ASP.NET is going
to either throw an exception for OutOfMemory within a process or ASP.NET is
going to recycle a process that consumes too much memory. I would think this
is a
good thing for ASP.NET to do, especially if it's one process that's doing
this.
Imagine if you have 5 users hitting this one page.
I know what your thinking too, what's the difference between OutOfMemory
Exception
and recycling a process that consumes too much memory. I'm not clear on
that, but
either way too much memory is being consumed and there's got to be a process
that stops that otherwise the OS would go down and that's what ASP.NET is
doing.
Gabe
> The following script (which must be included in a .aspx file):
>
[quoted text clipped - 42 lines]
>
> Ole