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

  • Home
  • SEARCH
  • 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 970537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:49:17+00:00 2026-05-16T02:49:17+00:00

When I run the following code, it slowly eats up my memory and even

  • 0

When I run the following code, it slowly eats up my memory and even starts using swap:

 long long length = 1024ull * 1024ull * 1024ull * 2ull; // 2 GB

 db = [NSMutableData dataWithLength:length];

 char *array = [db mutableBytes];

 for(long long i = 0; i < length - 1; i++) {
      array[i] = i % 256;
 }

If I run it without the for cycle no memory is used at all:

 long long length = 1024ull * 1024ull * 1024ull * 2ull;
 db = [NSMutableData dataWithLength:length];
 char *array = [db mutableBytes];
 /* for(long long i = 0; i < length - 1; i++) {
      array[i] = i % 256;
 } */

I can only conclude that NSMutableData is only “reserving” memory and when it is accessed then it really “allocates” it. How is it done exactly?

Is this done via hardware (CPU)?

Is there a way for NSMutableData to catch memory writes in its “reserved” memory and only then do the “allocation”?

Does this also mean that a call to [NSMutableData dataWithLength:length] can never fail? Can it allocate any size of memory using swap to get it if needed?

If it can fail will my db variable be null?

In apple’s “NSMutableData Class Reference” I have seen only vague sentences about these topics.

  • 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-16T02:49:18+00:00Added an answer on May 16, 2026 at 2:49 am

    This is not so much an NSMutableData issue but a kernel/OS issue. If the process requests a (big) chunk of memory, the kernel will normally just say “that’s ok, here you go”. But only on actually using it, it is really (“physically”) allocated. This is ok since if your program starts with a 2 GB malloc (as you are doing here) it would otherwise instantly push out other programs to swap, while in practice you will often not use the 2 GB right away.

    When accessing a memory page that is not actually present in physical memory, the kernel will get a signal from the CPU. If the page should be there (because it is within your 2 GB chunk) it will be put in place (possibly from swap) and you will not even notice. If the page shouldn’t be there (because the address is not allocated within your virtual memory) you will get a segmentation fault (SIGSEGV or EXC_BAD_ACCESS kind of error).

    One of the related topics is “overcommit(ment)”, where the kernel promises more memory than is actually available. It can cause serious problems if all processes start using their promised memory. This is OS dependent.

    There are a lot of pages on the internet explaining this better and in more detail; I just wanted to give a short intro so you have the terms to put in google.

    edit just tested, linux will easily promise me 4 TB of memory, while – I assure you – there is not even 1 TB of total disk storage in that machine. You can imagine that this, if not taken care of, can cause some headaches when building mission critical systems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 473k
  • Answers 473k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can dynamically generate <tr> element to represent row in… May 16, 2026 at 3:48 am
  • Editorial Team
    Editorial Team added an answer Good to find a fellow country man :) Just tested… May 16, 2026 at 3:48 am
  • Editorial Team
    Editorial Team added an answer You need to use a UNION to get this to… May 16, 2026 at 3:48 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.