|
-
Aug 13th, 2010, 09:53 AM
#24
Re: Threading and leaking memory - a demonstration
I may be missing something, here, but isn't the result of this whole exercise a case of 'you don't get something for nothing'?
If you are running lots of very short lived tasks, why create a thread for each task? Why not create a single thread and queue those tasks?
It seems like you are making a shopping list and driving to the store, getting a single item then driving home; getting back in the car to get the next item on the list, driving to the store, and so on. As you normally do, you get all the items in one trip.
The thread pool, however, is designed to do that: the ability to create threads very quickly on demand. But even that takes resources - that's why servers tend to be multi-processor with a huge dollop of RAM.
While the 'standard' philosophy is to create and access resources when you need them, if the act of simply creating the resource and it's requirements is expensive, then creating it ahead of time can be beneficial.
You could create a single thread which accesses a queue of objects to work on. If there's no objects then it sits there, idling in a do/loop or some such. Your main thread (or threads) then put these objects in the queue to be worked on, the worker thread signaling when each item is complete.
Also, I don't think there is a memory leak as such: yes, memory appears to be consumed, but the framework is not throwing things away because you keep needing the memory very, very quickly. Disposing the objects can be expensive also, so rather than kill the application performance to take a timeout to clean up, it just assigns it more memory.
In other words, considering a 'shopping list' example, rather than waiting for the first car to come back from the store, a new car is used to get the second item, and another for the third and so on. Since each car can be only used once (it's a thread that has completed), you can either take time out to sell the car or get another car for the next item on the list. To maintain performance (getting items on the list) the junk heap of cars is going to get pretty big before there's time to dispose of them.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|