Avatar of SynthSubs
 asked on

Massive allocations with with CFString while writing to NSOutputStream in iOS

I have problem with large number of allocations while writing to NSOutputStream. I am using few classes together that are supposed to stream data from AudioUnit to remote server. Code below:


Data *data = [Data sharedData]; //it's shared data I use to store information for the final socket

intFromBuffer = audioBufferList->mBuffers[0].mData;
NSUInteger length = audioBufferList->mBuffers[0].mDataByteSize;

NSMutableData *dataOut = [[NSMutableData alloc] initWithCapacity:0];
[dataOut appendBytes:(const void *)intFromBuffer length:length * sizeof(SInt16)];

[data setOutput:dataOut]; //it writes data to the shared data

Open in new window

Data.m (for singleton)

@implementation Data
NSMutableData *output;
NSMutableData *input;

@synthesize output;
@synthesize input;

+(id)sharedData {
static Data *sharedData = nil;

@synchronized(self) {
    if (sharedData == nil) {
        sharedData = [[self alloc] init];
    return sharedData;

-(void) setOutput:(NSMutableData*)outputt{
    output = outputt;

-(void) setInput:(NSMutableData*)inputt{
    input = inputt;


Open in new window


- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
if ([keyPath isEqualToString:@"output"]) {
    Data *data = [Data sharedData];
    [self writeOut:data.output];


- (void)writeOut:(NSString *)s {
    NSString *dataTo = [NSString stringWithFormat:@"%@\n", s];
    NSData *data = [[NSData alloc] initWithData:[dataTo dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];

Open in new window

Also NetworkCommunication is running in Network Thread in my app.

It generally creates great amount of data, which in 2,5 min was about 200 MB in virtual memory. I tried to simply pass stringWithFormat: directly to my writeOut: function however it gives nothing, not idea how to deal with it.

If someone asks why I use stringWithFormat, simply because I need to put \n for server to read the message.

Any help much appreciated.

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Your help has saved me hundreds of hours of internet surfing.