views:

124

answers:

3

Windows Server 2008. How can I quickly use up RAM so to induce GC in my app. If there is a way to do it without needing Visual Studio or installing a language runtime it would be good.

EDIT: I don't want to have to write an app and then copy it over to the server. I'm looking for a way to do it quickly without writing an app that requires an IDE or installation of a runtime/compiler. Perhaps a powershell or batch script?...

A: 

You could run your program inside a virtual machine such as Virtual Box, where you specify the memory ceiling of the guest operating system.

I'm having trouble imagining a scenario where this would be necessary though. Could you provide more information about the problem?

Mike
A: 

If you are using java you can specify the max amount of memory using Xmx. Search for JVM memory setting

Nick
+3  A: 

I don't think using up RAM outside your process is going to necessarily trigger GC.

If I understand your question correctly, you have a program Foo.exe that is written in some unknown language, running on some unknown runtime (are you not allowed to post the details for some reason, or do you just not know?), and you want to try to get that program's runtime to trigger a garbage collection. However, you want to do this by using up RAM outside of foo.exe.

You could do this by creating a simple batch file that just started up a hundred copies of IE or Word or whatever program you want. However, I don't think that will do what you want it to do. If your process has already allocated a certain amount of memory, it won't necessarily give that memory up or trigger GC just because other processes are being started. It may page to disk, or may force other programs to page to disk. But not all Garbage Collectors are alike, so we can't really help without more details. I'm pretty sure some VM's never give back memory once they've allocated it, even after GC.

Peter Recore
Thanks Peter. So an external shortage of memory (external to my app) won't induce garbage collection. My mistake.FWIW, I'm on the .NET runtime in IIS (ASP.NET MVC app). I've a LINQ2SQL datacontext which is instantiated using Windsor IoC container on a per-web-request basis. For some reason the LINQ2SQL datacontext is not getting GCed naturally and is causing OutOfMemory exceptions in my app. If I call GC manually when I am done with the datacontext the memory leak goes away.
Mr. Flibble
@Mr. Flibble: `DataContext` implements `IDisposable`. Are you properly disposing of it (e.g. with a `using` block)?
Daniel Pryden
Daniel, it isn't in a `using` block but I have tried calling `.Dispose` on it but it didn't have an effect. Calling GC does have an effect without calling `.Dispose`
Mr. Flibble
I would lay very good odds that your problem is that somewhere you have an `IDisposable` object that you aren't disposing of. The GC only manages "managed" allocations. In your case, you're doing LINQ to SQL, so your SQL Server driver will allocate big data objects directly from the OS (as "unmanaged" memory). The GC *cannot* release those objects back to the OS. All the GC can do is release the managed object that is holding a pointer to the unmanaged object. If you're lucky, the managed object will have a finalizer, and forcing GC will force that finalizer to run and release the object.
Daniel Pryden
But the whole reason the `IDisposable` interface exists is so you can *explicitly* release the unmanaged resources held by a managed object. So you should just go through your code, and every time you construct any object that implements `IDisposable`, make sure it's in a `using` block, or else that there's a `finally` clause that calls `.Dispose()`.
Daniel Pryden
@Daniel My problem (apart from that calling Dispost doesn't seem to work) is that I have no clue where to start a `using` block when the instance is created using Windsor IoC container. I guess I need to do some research.
Mr. Flibble
@Mr. Flibble: I think the real answer is that you probably shouldn't be creating `IDisposable` objects with an IoC container. This question may be useful to you, though: http://stackoverflow.com/questions/987761/how-do-you-reconcile-idisposable-and-ioc
Daniel Pryden
BTW, Peter: Sorry to hijack the comments on your answer like this. :-) Thanks for understanding...
Daniel Pryden
Part deux:http://stackoverflow.com/questions/1710992/linq-to-sql-datacontext-windsor-ioc-memory-leak-problem
Mr. Flibble
@Daniel - no problem. You are chiming in with lots of good details. my answer was pretty high level.
Peter Recore