0

I have a service, that I query once in a very long while, and i would like to "streamline", or improve the efficiency, of its memory allocation.

Most of the time it just sits and waits, and once in awhile it gets a request that requires allocating a lot of memory, and do some processing on it. I don't know the types or structure in advance - it depends on the request, and varies wildly.

Now, the big processing request is precluded by some chatter (other requests), that might take a few seconds.

What I want to do is, when the chatter (smaller requests) start, say to the .Net Framework: go to windows, and get yourself a couple of GB's of memory so it'll be available faster when i ask, and when I'm done, say to the .Net: everything I'm not currently using, you can give back, because I'm not going to need it for a while.


I'm starting profiling as we speak... but I suspect it would be part of the issues that could improve.

I'll try to clarify the situation.

I have a service that sits on a server and 95% of the time just does nothing. Once in a long while it gets a request to do some mostly memory intensive processing.

I know a little bit of time in advance that it's all going to happen.

All i want to do, is hint the GC "Were going to need a lot of memory soon" and later "Were not going to need anything special for a while"


OK. I've done profiling, and decided I don't care about this. The allocation does take some time (several to several dozens milliseconds), but it's insignificant versus the rest of the processing...

regarding the releasing part, it happens eventually, and doesn't really interfere with the rest of the server...

AK_
  • 7,981
  • 7
  • 46
  • 78
  • 3
    Do you mean memory allocation happening in bursts are causing a measurable performance problem in your application? Otherwise, how do you know the heap manager and the garbage collector are not doing exactly what you want under the hood? – Frédéric Hamidi Apr 05 '13 at 13:20
  • 4
    You are not going to be able to optimise the garbage/memory management of the framework. It is already exceedingly good... Also, it is difficult to offer any advice on you current scenario as the question is very vague. – MoonKnight Apr 05 '13 at 13:21
  • And how do you know that the bottleneck is the memory allocation ? – mathieu Apr 05 '13 at 13:22
  • Just make sure you're disposing your disposables. The garbage collector will handle the rest. If you're having performance problems, look elsewhere for things to optimize. Preallocating a couple gigabytes of memory won't fix it. – JosephHirn Apr 05 '13 at 13:27
  • @FrédéricHamidi I can see now the allocation takes some time, and i think it could be faster, because i know in advance when its going to happen. – AK_ Apr 05 '13 at 13:33
  • @Killercam I don't want to optimise .Net's GC , I just want to give it more information on my needs... – AK_ Apr 05 '13 at 13:34
  • 1
    Allocating memory is *exceedingly* fast (so long as you have enough of it to spare). I'm quite confident that it's not your bottleneck. – Servy Apr 05 '13 at 13:41
  • @Servy I'm talking about several objects hundreds of MB's in size... – AK_ Apr 05 '13 at 13:47
  • 3
    @AK_ And I'm saying it doesn't matter. Allocating memory is not dependent on the amount of memory allocated. Allocating one byte is no faster than allocating 100 MB. – Servy Apr 05 '13 at 13:49

2 Answers2

1

If you want to be able to reserve a chunk of memory for your uses then please see:

allocating "unmanaged" memory in c#

Note, doing so can be risky and the Garbage Collector and memory allocation in the .NET VM is already rather good.

If the memory allocation can be largely cached then I'd recommend caching what can be done so with WeakReference such that quick successive requests could benefit from accessing the cached data, but if a garbage collection comes in between requests spaced a decent amount apart then the data can be released and just re-created in the next request.

See: Weak reference benefits

And: http://msdn.microsoft.com/en-gb/library/system.weakreference.aspx

Community
  • 1
  • 1
Clint
  • 6,133
  • 2
  • 27
  • 48
  • 1
    1. I don't need unmanaged memory and i don't want to start allocating it into objects myself. 2. Caching won't help me, I'm actually looking for the opposite, after the processing is done i dont need that data anymore... – AK_ Apr 05 '13 at 13:36
  • @AK_ then apart from calling `GC.Collect()` there's not much else you can do, `Collect` will inform the garbage collector you'd like some memory cleaning up, but it's entirely down to it when and how much it decides to do. – Clint Apr 05 '13 at 13:38
  • from what I've seen so far, `GC.Collect()` mostly ignores me... – AK_ Apr 05 '13 at 13:43
  • @AK_ aha yes, it will mostly appear that way, it's intended to merely be a hint to the GC that it might want to come in and clean up, but if it thinks otherwise you won't get the collection you're after. Here's why: http://stackoverflow.com/questions/11354086/gc-collect-not-collecting-immediately – Clint Apr 05 '13 at 13:48
0

The GC is most of the time smart enough to do this for you. However, it is an architectural problem and can be dealt with by modifying the flow of activities in the service.

e.g. you can allocate the objects required for processing big request in advance before the request comes. However, for deallocating, either explicitly implement idisposible interface to them and destroy them once they are used or leave it to GC.

Further, you have to understand how memory allocation works. In order to get memory allocated for .Net objects, you must be knowing the type of object in advance. Just allocating plain block of memory is in no way helpful to you. Most of the time the object creation or cloning code is more resource consuming compared to the mallocs that the framework uses to allocate memory for the object.

Considering the details, I would say that even if you could successfully do this routine, it would become much more complex and might add few more bugs to your code. Better leave this to .Net framework. It is very good at allocating and deallocating memory.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • I like the GC most of the time :-) . The thing is, it's a **lot** of memory it needs to do malloc to. I just want it do the malloc a little bit in advance, and the free as soon as possible. – AK_ Apr 05 '13 at 13:39
  • 1
    @AK_ Allocating memory is very cheap; it's the deallocations that are expensive for the GC, so trying to do this won't really help you. Odds are high that you'll end up just making it worse trying to manage it yourself. – Servy Apr 05 '13 at 13:42
  • I **don't** want to manage it myself, though i know i could, it'll be too much work. as I said, I just want to hint the GC how to do it better, if possible... – AK_ Apr 05 '13 at 13:45
  • 1
    @AK_ 1) That's *not* possible, as several people have already told you 2) It still wouldn't help anything. – Servy Apr 05 '13 at 13:51