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 1105539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:38:54+00:00 2026-05-17T01:38:54+00:00

Just to test, I ran this code #include<unistd.h> #include<stdio.h> #include<stdlib.h> int main() { int

  • 0

Just to test, I ran this code

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>

int main() {

    int **ar = (int**) malloc(100000000* sizeof(int*));

    int i;
    for(i = 0; i<10000000; i++) {
        ar[i] = (int*) malloc(1000 * 4);
        ar[i][123] = 456;
    }

    usleep(3000000); usleep(3000000);
    usleep(3000000); usleep(3000000);
    usleep(3000000); usleep(3000000);

    return 0;
}

The memory usage graph went like this (the bottom pink colored graph tracks memory).

alt text

Though this program did not run out of memory, any more memory requirements would lead to the malloc failing and then a segmentation fault due to the ar[i][123] = 456; line.

I want to put a limit on the memory allocation through my program, but also do not want to bind my program statically.

For example, Is there a way to tell my program to use atmost half of the memory available on the system (the system where the binary was run), but no more?

So, if my program is run on a machine with 8GB of memory, it can use a max of 4GB, but in case the program runs on a machine with 256MB, only 128MB should be available to the program.

Also, I want to do this from within my program, rather than controlling the amount of memory available using some external utility.

  • 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-17T01:38:54+00:00Added an answer on May 17, 2026 at 1:38 am

    It’s generally considered abusive for a program to attempt to detect the amount of physical memory on a machine and use a certain portion of that. What if 10 programs each think they’re entitled to make use of half the machine’s memory?

    A much better approach to dealing with huge volumes of data would be to work with it on disk, and rely on the operating system’s filesystem cache to make optimal use of the machine’s physical memory in a way that’s fair to multiple processes.

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

Sidebar

Related Questions

I made a test file, and ran through the excel file just fine, but
How would you test this scenario? I've just started looking into NHibernate and having
I have a trivial console application in .NET. It's just a test part of
I just finished a test as part of a job interview, and one question
I've just done a test with bitfields, and the results are surprising me. class
I just added xUnit to our test project (for the Asserts, we're still using
Just some days ago I started looking into a unit test framework called check,
I just lost 50% of my answer on a test because I wrote the
We have just 'migrated' an SQL Server 2005 database from DEVEL into TEST. Somehow
When I am creating a test project in Visual Studio, do I just create

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.