Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
As a follow-up, I got a chance to run my posted code under Instruments on 10.6.2, and I don't see any of the OP's originally reported problems. There is no unexpected memory consumption, and no leaks.

The conditions of the test runs were as follows.

First, I generated a 50k-line data file as input:
Code:
java -Dlines=50000 RanguvarGen abc.txt
The exprs.txt file contained 3 expressions, so there will be a total of 3 iterations of main()'s loop. This was borne out by observation.

Next, I compiled the .m file for i386 and x86_64. This made no difference to the test runs. There was similar memory usage in both cases.

In Instruments.app I choose the Leaks template, which includes object alloc information and leak detection. No leaks were ever detected in any test run.

When I ran the program, the memory consumption plainly increases at each of the 3 iterations of the loop. This is exactly as expected.

The increase corresponds to the call to grepFileForExpression(), which greps the entire file and collects results by reading and parsing a single NSString. There are a large number of NSStrings, and a moderate number of NSArrays during this time. The NSStrings occupy the largest amount of memory, by far. This is exactly as expected, given the algorithm.

Memory usage then drops down to approximately its starting value, which would correspond to the draining of the pool in the loop.

When the loop ends, there is very little memory still allocated, and none that can be pinpointed as belonging to NSTasks.

This situation doesn't change when the code is modified for a small input file (40 lines) and a large number of iterations (1000). The code changes to do this are as follows:
Code:
#define MAX_ITER	1000

int main( int argc, const char * argv[] ) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	NSArray *values = exprsArray( @"./exprs.txt" );  // pathname may contain ~/
	const int count = [values count];

	// Choose larger of count or MAX_ITER
	const int limit = (count < MAX_ITER) ? MAX_ITER : count;
	NSLog( @"Starting %d iterations", limit );

	for (int i = 0; i < limit; i++) 
	{
		NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
		NSArray *possibleMatches = grepFileForExpression([values objectAtIndex:i % count]);
		NSLog( @" possible count: %d", [possibleMatches count] );
		[pool drain];
	}
...etc...

There is still the same cycle of memory consumption during the grepping, and subsequent cleanup that reduces memory consumption. The magnitude is less because the input file is smaller, but there are many more cycles, so any cumulative effects from NSTask would still show up. None were observed.


In conclusion, I don't see any problems.

I was unable to replicate the the OP's reported problem, because insufficient code was posted to run a test entirely from the OP's code.

Since I wrote some of the code I tested, it's possible that something I wrote differs from what the OP wrote, and that difference is the cause of the OP's observed problem. That's just a guess, because without code to run, there's no way to observe anything.
 
Yeah, the problem is definitely very weird. If I just run my code normally, without calling the posted function, everything works as expected (no growing memory consumption).
If I run only a loop, calling the function every time, like chown33 properly stated, there are no problems, either.
The problem surprisingly only comes up if I call the function from certain points in my code, even if I then do nothing with the returned results. Again, I'd love to post the code, but it's too much and apparently it's impossible to condensate the problem down to few lines of code.

I see that I can't except help like this though, so I guess I'll have to track down the problem myself.
Thanks for all the help though, I will try to improve my coding and my question-posting style now!

EDIT: When running my code with Instruments (ObjectAlloc tool), the *All Allocations* 'Overall Bytes' value stays at about 400KB while the code runs.
However, when run normally via the Terminal, the code does the weird memory-eating problem. Does the 'Overall Bytes' value describe the total memory usage of the code, or is it something like "bytes allocated per second"?
 
Excuse my Visual Basic 6 thinking:)() But have you set up break points at suspected code and tried that appraoch to find leaks?

The problem with a memory leak is that it is usually not caused by the code you execute, but by the code that you didn't execute :rolleyes:

For a good way of finding memory leaks google for "Rubber duck programming" :D In this case it seems there was no memory leak, just lots of memory used by an inefficient algorithm.
 
The problem with a memory leak is that it is usually not caused by the code you execute, but by the code that you didn't execute :rolleyes:

I meant to say blocks of code (In visual basic, it was very often just a bunch of functions and a lot of Call XYZs). That way you can find out what blocks of omitted code is causing the problem.

For a good way of finding memory leaks google for "Rubber duck programming" :D In this case it seems there was no memory leak, just lots of memory used by an inefficient algorithm.

Does it happen to include coding in a bath tub? :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.