|
1.
I have no idea what all that means.
2.
I'm not a PowerShell user, nor will I become one any time soon.
I have been reading up on it a bit, and seem to have hit on two reasons for it to leak memory:
one of the first results googling "C# powershell memory leak"[^]
leaky PowerShell scripts[^]
3.
If I were to expect lots of output from something like PowerShell, and having seen the number of questions and complaints on it after a 1 minute Google, I would opt for a file interface: launch it with Process.Start() and have it create a file, hence avoiding most potential trouble.
4.
I recommend you reduce your program to a fraction of the intended functionality, make the memory consumption numbers very visible, and work on it till your "climbing slowly" is completely gone. Then iteratively add code and functionality, keeping a sharp eye on the memory situation at all times.
|
|
|
|
|
edit: Piebald and others raised concerns here about the possibility that the use of IEnumerable<T>Count() here would make the code break with Stack and Queue.
That is not the case; the code works:
private void TestChunking()
{
string testString = "aaabbbcccdddeeefffggghhh";
int[] intary = new int[36];
List<int> intlist = new List<int>(36);
Stack<int> stack = new Stack<int>();
Queue<int> queue = new Queue<int>();
for (int i = 0; i < 36; i++)
{
intary[i] = i;
intlist.Add(i);
queue.Enqueue(i);
stack.Push(i);
}
var result1 = intary.ToChunkedKvPList(9);
var result2 = intlist.ToChunkedKvPList(6);
var result3 = stack.Reverse().ToChunkedKvPList(9);
var result4 = queue.ToChunkedKvPList(4);
var result5 = testString.ToChunkedKvPList(3);
} The goal here (Extension method on IEnumerable) was to take an IEnumerable of any Type, and a chunk-size, and return a List of KeyValuePairs where each KeyValuePair had as its 'Key the first element in a chunk, and the KeyValuePair 'Value contained the all-but-the-first element in the chunk:
public static class IEnumerableExtensions
{
public static IEnumerable<KeyValuePair<T1, List<T1>>> ToChunkedKvPList<T1>(this IEnumerable<T1> source, int chunksz)
{
if(source.Count() % chunksz != 0) throw new ArgumentException("Source.Count must equal ChunkSize modulo 0");
int ndx = 0;
int listsz = chunksz - 1;
return source
.GroupBy(x => (ndx++/chunksz))
.Select(grp => grp.ToList())
.Select(lst => new KeyValuePair<T1, List<T1>>(lst[0], lst.GetRange(1, listsz)));
}
} Yeah, this works, but I remain convinced there is probably a much more elegant way of doing this using Linq; a way that would not require using an indexer external to the Linq operation. Perhaps a way to avoid two levels of 'Select ?
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 12-Jan-16 6:18am.
|
|
|
|
|
You can avoid the first Select like this:
return source
.GroupBy(x => (ndx++ / chunksz))
.Select(grp => new KeyValuePair<T1, List<T1>>(grp.First(), grp.Skip(1).ToList()));
This also makes the variable listsz obsolete.
I don't see a way to get rid of the external indexer without making it more convoluted.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Thanks for this excellent response, Sascha.
It would be interesting to know if the use of 'First and 'Skip makes for any difference in computation-time and memory use compared to the code I showed. I doubt it.
cheers, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 11-Jan-16 14:40pm.
|
|
|
|
|
You're very welcome, Bill. Best of luck for your eyes surgery!
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
You could also get rid of the second Select :
return source.GroupBy(
x => (ndx++ / chunksz),
(key, grp) => new KeyValuePair<T1, List<T1>>(grp.First(), grp.Skip(1).ToList()));
Enumerable.GroupBy(TSource, TKey, TResult) Method (IEnumerable(TSource), Func(TSource, TKey), Func(TKey, IEnumerable(TSource), TResult)) (System.Linq)[^]
Add in a KeyValuePair<TKey, TValue> factory method:
public static class KeyValuePair
{
public static KeyValuePair<TKey, TValue> Create<TKey, TValue>(TKey key, TValue value)
{
return new KeyValuePair<TKey, TValue>(key, value);
}
}
and the statement becomes almost readable:
return source.GroupBy(
x => (ndx++ / chunksz),
(key, grp) => KeyValuePair.Create(grp.First(), grp.Skip(1).ToList()));
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'd actually prefer the separate .Select over the .GroupBy with resultSelector, to me that's a split second faster to recognize. I like the idea with the factory method though
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
thanks for this ! Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
I suspect that would be so much easier (and quicker) in straight procedural code.
And IEnumerable doesn't have a Count member; so you're doomed to failure from the first statement. Maybe you want to use IList instead? I think a better behaviour would be to not attempt to count the items, but to leave the final item short or to pad the final item with default(T1) s . Maybe even allow the caller to specify which behaviour to use (throw, pad, as-is). And, of course, document such behaviour.
|
|
|
|
|
PIEBALDconsult wrote: And IEnumerable doesn't have a Count member;
:cough: MSDN[^] :cough:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
But that's an extension method, and it would consume the IEnumerable while counting.
|
|
|
|
|
That's an interesting comment: the word "consume" usually means "use-up;" but, in this case, the code works, and works because a source IEnumerable can be "used" any number of times.
Of real interest is whether multiple evaluations of the IEnumerable source are very expensive ... in terms of memory, time.
Perhaps it is the case that transforming the IEnumerable to a List<T;> is a good thing to do, if it needs to be evaluated more than once.
thanks, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
BillWoodruff wrote: because a source IEnumerable can be "used" any number of times.
Not all of them; and you can't tell. Queue and Stack implement IEnumerable, but they can be consumed only once (fortunately they have Count properties).
BillWoodruff wrote: whether multiple evaluations of the IEnumerable source are very expensive ... in terms of memory, time.
It may have to enumerate it fully; that takes time. Enumerating may also involve file or network access or similar (e.g. database access, reading from a socket) that uses time, IO, and memory.
And this particular result is not worth the effort in this case; so it's a waste.
Of course, it's possible that the Count method you use checks for certain types (e.g. Stack, Queue, Array, String) or interfaces (e.g. IList) and then uses the appropriate Count or Length methods rather than enumerating, but failing that, it must enumerate.
But the bottom line, in this case, is that there is no reason to check the Count anyway. And as Luc pointed out, if you're checking the Count you might as well check the chunksz before trying to divide by it. Which then leads to the question "what to do when the caller specifies a chunksz of zero?" -- and I suspect the "best" thing to do is to treat it as an "all the rest" value. But that's just my thought.
I suggest leaving the burden of checking such things to the caller. Document what the method does and let the buyer beware.
modified 9-Jan-16 23:34pm.
|
|
|
|
|
Thanks for the interesting response, the "quick sketch" I showed here was not meant to show all the programmer-is-an-idiot-proofing that might go in "production code."
I'll follow up on your comments by doing some testing with Queues and Stacks; never even thought of trying those.
cheers, Bill
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
Be sure to hydrate.
|
|
|
|
|
PIEBALDconsult wrote: Queue and Stack implement IEnumerable, but they can be consumed only once
The enumerators for both Queue<T> and Stack<T> will not remove items from the collection, so you can iterate them as many times as you want.
BlockingCollection(T).GetConsumingEnumerable[^] is a better example.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
What Piebald said.
I believe the best examples of IEnumerables that would be consumed are Iterator Methods (using yield return) and Enumerable.Range(,).
<Edit>removed some brain rot<Wrong is evil and must be defeated. - Jeff Ello
modified 11-Jan-16 12:32pm.
|
|
|
|
|
That looks horribly inefficient!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Worse than that, it doesn't work as intended.
Note to self, never post untested code you thought up before going to bed.
|
|
|
|
|
I realized from Piebalds response below that I owe you a proper answer.
If you take a look at the IEnumerable(T) Interface[^] it specifies only one Method, GetEnumerator.
All other methods are extensions that depend on that one single method.
So if we look at the IEnumerator(T) Interface[^] you'll notice that it has only three methods.
Dispose is of no interest here. MoveNext means that it's a forward only enumerator.
But note that Reset does not need to be implemented. which means you cannot count on getting restarted. So you can only count on enumerating once.
This is why I wrote that "it would consume the IEnumerable while counting".
So why did your code work?
I guess your Source parameter also implemented the ICollection(T) Interface[^] which specifies the Count property. (A List<T> for example)
And if you have a Class property/method with the same signature as an Extension method, the Class property/method will always take priority.
The compiler never complained since it could see the Extension method Count()
Therefore I should have written "it might consume the IEnumerable while counting" or maybe rather "might enumerate".
Luckily the extension method Count and the Class property Count, does the same thing.
BillWoodruff wrote: Perhaps it is the case that transforming the IEnumerable to a List<T;> is a good thing to do, if it needs to be evaluated more than once. In general I'd say yes, but it probably depends on whether the cost of instantiating objects or saving memory is of the highest importance.
|
|
|
|
|
BillWoodruff wrote: Perhaps it is the case that transforming the IEnumerable to a List<T;> is a good thing to do, if it needs to be evaluated more than once.
Probably not, as that may copy all of the elements, which is just not worth the effort in this case.
I say again; either stop trying to get the Count, or change the parameter type to IList -- it is clear that you (Bill) do not want an IEnumerable at all for the method presented.
Actually, it's good that this discussion came up now because for the last few weeks I have been tweaking some Extension Methods that were accepting IEnumerable and I was concerned about what could be sent in. I have now changed the methods to specify IList and I think everything will be much better.
One of the problems I definitely had when specifying IEnumerable was that String implements IEnumerable, but I did not want to treat it the same as other IEnumerables, which meant testing for is string all the time. By changing to IList (which String does not implement), I no longer need that test.
|
|
|
|
|
PIEBALDconsult wrote: it is clear that you (Bill) do not want an IEnumerable at all for the method presented. That's not correct; I wanted the method to work with Type 'String; and, it does.
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
modified 11-Jan-16 14:11pm.
|
|
|
|
|
Well, that's alright then.
|
|
|
|
|
Thanks for the full response, Jorgen !
«Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin
|
|
|
|
|
Jörgen Andersson wrote: would consume
I suggest "may have to enumerate" as a more accurate word choice.
|
|
|
|
|