Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6578075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:43:26+00:00 2026-05-25T15:43:26+00:00

Possible Duplicate: Most memory efficient way to split an NSString in to substrings I’m

  • 0

Possible Duplicate:
Most memory efficient way to split an NSString in to substrings

I’m trying to split a 20Mb string. I’ve tried using componentsSeparatedByString but it consumes too much RAM. I think that this is down to the fact that it splits the string but also leaves the original string intact. This means that the string is effectivly stored in memory twice (even if I release the original string right after the split it is still an issue.)

I’m very new to Objective C. I’ve tried to write some code that removes the substring from the original string as it adds it to the array of found strings. The idea is that as the mutable array of found strings gets larger the original string gets smaller. The only problem is that it leaks memory and crashes. If someone could tell me what I’m doing wrong then that yould be great!

    NSRange range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];
    int counter = 1;

    // locations will == int max if it can't find any more occurances
    while (range.location < [mainHtml length]) {
        NSString *curStr;
        NSRange curStrRange;

        NSRange rangeToSearchIn = NSMakeRange(range.location+1, [mainHtml length] - range.location - 1);
        NSRange nextRange = [mainHtml rangeOfString:@"<p class=NumberedParagraph>" options:NSCaseInsensitiveSearch range:rangeToSearchIn];

        if (nextRange.location > [mainHtml length])
        {
            // This is the last string - get everything up to the end of the file
            curStrRange = NSMakeRange(0, [mainHtml length]);
            curStr = [mainHtml substringFromIndex:range.location];
        } else {
            curStrRange = NSMakeRange(range.location, nextRange.location - range.location);
            curStr = [mainHtml substringWithRange:curStrRange];
        }

        // Remove the substring just processed from the orignal string
        // * it crashes here, normally on the 3rd itteration
        mainHtml = [mainHtml substringFromIndex:curStrRange.location + curStrRange.length];
        range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];

        [self.parts addObject:curStr];
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:43:26+00:00Added an answer on May 25, 2026 at 3:43 pm

    I think that @babbidi had the correct idea. mainHtml is large and you have many autoreleased copies of it around (one copy for each iteration) that are not being released. Try adding the following @autorelease in your code to release all the autoreleased objects at the end of each loop. If you are not using Mac OS X 10.7 then you need only create the autorelease pool manually outside the main loop and drain it once per iteration.

    NSRange range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];
    int counter = 1;
    
    // locations will == int max if it can't find any more occurances
    while (range.location < [mainHtml length]) {
        @autorelease {
            NSString *curStr;
            NSRange curStrRange;
    
            NSRange rangeToSearchIn = NSMakeRange(range.location+1, [mainHtml length] - range.location - 1);
            NSRange nextRange = [mainHtml rangeOfString:@"<p class=NumberedParagraph>" options:NSCaseInsensitiveSearch range:rangeToSearchIn];
    
            if (nextRange.location > [mainHtml length])
            {
                // This is the last string - get everything up to the end of the file
                curStrRange = NSMakeRange(0, [mainHtml length]);
                curStr = [mainHtml substringFromIndex:range.location];
            } else {
                curStrRange = NSMakeRange(range.location, nextRange.location - range.location);
                curStr = [mainHtml substringWithRange:curStrRange];
            }
    
            // Remove the substring just processed from the orignal string
            // * it crashes here, normally on the 3rd itteration
            mainHtml = [mainHtml substringFromIndex:curStrRange.location + curStrRange.length];
            range = [mainHtml rangeOfString:@"<p class=NumberedParagraph>"];
    
            [self.parts addObject:curStr];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Implement Stack using Two Queues I want the most efficient way in
Possible Duplicate: most elegant way to return a string from List<int> I'm not sure
Possible Duplicate: The most efficient way to implement an integer based power function pow(int,
Possible Duplicate: What is the most efficient way to clone a JavaScript object? I
Possible Duplicate: What is the most efficient way to clone a JavaScript object? How
Possible Duplicate: The most efficient way to implement an integer based power function pow(int,
Possible Duplicate: Most efficient implementation of a large number class Suppose I needed to
Possible Duplicate: What's the most reliable way to prohibit a copy constructor in C++?
Possible Duplicate: Easy to use/learn PHP framework? Do most of professional programmers recommend using
Possible Duplicate: What is the most straightforward way to pad empty dates in sql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.