1

I want to assign large amount of character from a file to a NSString. I did that. It is working fine for me in iOS simulator. But crashed in iOS devices. I think, length exceeds the NSString memory. I can realize that NSString limit will be change with different devices and available memory. My question, Is there any alternative to get the string from file and store it in the runtime with out memory issue?. or How to check NSString's available length?

Here my code. I am appending the parsed string in NSString

 Str=[Str stringByAppendingString:TmpStr];  
SARANGA
  • 172
  • 1
  • 8

3 Answers3

2

You may want to confirm it's actually a memory issue and handle the low memory condition. See this SO post on how to do that: iPhone - How to deal with low memory conditions

If you're trying to load a books content at once into an NSString it will likely run into out of memory conditions on memory constrained devices. A stream allows you to read the file a portion at a time and only hold the current portion of the file in memory. Even if you can successfully load some books completely, it will be slower and still consume much more memory than the app needs to - which will make your app one of the first to get flagged in low memory conditions.

Checkout Apple's Stream Programming Guide

From that guide:

Cocoa includes three stream-related classes: NSStream, NSInputStream, and NSOutputStream. NSStream is an abstract class that defines the fundamental interface and properties for all stream objects. NSInputStream and NSOutputStream are subclasses of NSStream and implement default input-stream and output-stream behavior. You can create NSOutputStream instances for stream data located in memory or written to a file or C buffer; you can create NSInputStream instances for stream data read from an NSData object or a file.

EDIT: In the comment you clarified you're loading from XML. You still have the same fundamental issue, you're trying to load large contents of some sort of "book" into memory at a time. You're appending tmp string as you parse the xml so the effect is the same. So, you have to find a way to incrementally load the portion of the book you need. Luckily, you can tell NSXMLParser to use a stream.

This may help: Using NSXMLParser initWithStream: no parser delegate methods received

I also just found this SO post on the topic that may help: Objective-C: Reading a file line by line

Not sure about the details in your app but you could stream up to the current data you need (and a few pages on either side) or you could even stream and break apart the contents into "pages" in some temp files on disk as sort of a "pages cache".

Community
  • 1
  • 1
bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • Thanks. But I am not appending all string to NSString at a time. Actually I get the String from Xml file using NSXMLParser So I am not appending all strings at a time. – SARANGA Nov 27 '12 at 13:14
  • OK - would have been good to know in the question :) EDITing answer – bryanmac Nov 27 '12 at 13:21
  • i want to parse the XML file from the local directory. I already download it from my web server. So can i use this streaming concept for that??? – SARANGA Nov 28 '12 at 05:20
  • Yes, see the link above for "Using NSXMLParser initWithStream". That means the whole file won't be loaded into memory as the parser goes over it and calls your callbacks. But that also means you cannot concat a string in those callbacks because that's effectively loading it all into memory into your string. It means you have to either stream (and throw away) up to the the page of data you want (have to remember where you left off) and stop after that page. You could cache on either side onto disk into a file per page (page cache) or you could process once and break the file into chunks. – bryanmac Nov 28 '12 at 13:08
  • The other option it to break apart the source contents you get from the server or break it apart into smaller files (if you don't have control over contents on server) locally after downloading. – bryanmac Nov 28 '12 at 13:09
0

Will you please share the console log when your app crashes? As much I think that's not the real problem, problem is different bit. Meantime, you can try by using the database for storage. And, if possible please show the line where you're trying to assign your string.

Mohit_Jaiswal
  • 840
  • 6
  • 10
  • It is like the memory issue. App scrashed.but, there is no console log. – SARANGA Nov 27 '12 at 12:55
  • have you activate your zombie enabled, try that. Possibly the cases may be that string got deallocate or may be other component, so just check. :) – Mohit_Jaiswal Nov 27 '12 at 13:00
  • Also, as per your words, in simulator it's working fine , so please from there you mention the length of string. Anyways, due to some work, I have to let...but will see you later. :) – Mohit_Jaiswal Nov 27 '12 at 13:02
0

Use NSMutableString instead of NSString. that might help You!

Asif Mujteba
  • 4,596
  • 2
  • 22
  • 38