Re: Partially release NSMutableData
Re: Partially release NSMutableData
- Subject: Re: Partially release NSMutableData
- From: "A.M." <email@hidden>
- Date: Thu, 5 Aug 2010 13:18:55 -0400
On Aug 4, 2010, at 9:02 AM, Marcus Karlsson wrote:
> Hello.
>
> The subject of this thread is probably somewhat weird at first but let me explain.
>
> Let's say that I'm receiving data from the network and need to store it in a buffer. I need to store it until enough data has been collected, where what is considered "enough" is impossible to know beforehand. It is only possible to know by examining the data as it is received.
>
> Right now I'm using NSMutableData. It's a quite nifty class where I can easily append data in the end as it arrives.
>
> Once enough data has arrived it is used by the program and is then no longer needed. What I want to know is what would be the best practice in order to save memory by removing just the data that is no longer needed. So far I have three alternatives.
>
> 1. Instead of a single NSMutableData object store each chunk using NSMutableArray and remove them one by one as they are no longer needed.
>
> 2. Use a single NSMutableData object. Once enough data has arrived copy the data that may have been appended later to a new NSMutableData object and release the first object.
>
> 3. Use a single NSMutableData object. Once enough data has arrived move the data that may have been appended later to the beginning and adjust the capacity.
>
> Any opinions on which one to use or ideas on even better ones?
If you are moving large data across the network, I would recommend against NSMutableData for three reasons:
1) NSMutableData allocates 25% more space than you ask for. This is to optimize for the common case of allocation expansion, but if you are moving 1 GB, then the extra allocation can be a harmful waste.
2) If you expand beyond the allocated space, the whole byte-space will be reallocated to a new address, causing fragmentation and expensive copying, especially if you don't know how much data is coming along the line and you allocate a buffer which is too small.
3) Deleting or shifting bytes out of NSMutableData does not reduce its actual allocation size.
For these reasons, I was forced to write my own ring buffer of sorts using NSMutableArray to store custom data objects. Then, whenever a socket data callback is received, the data is simply retained in the array. Because the wire protocol identifies message element sizes, I was able to preallocate the data objects and copy from the kernel directly into the custom data objects to eliminate copying. In conclusion, there is no way to "partially release NSMutableData", but if your wire protocol is lightweight, it doesn't really matter.
Cheers,
M_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden