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 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
  • 1 View
  • 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

just trying to test for equality in this piece of code, but getting a
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
Lets say I have hierarchy like this (This is just a test program. Please
Recently while trying to answer a questions here, I ran some test code to
This code copied anywhere else seems to work. It's just inside my app where
I am just learning how to use perl hashes and ran into this message
I just ran into this situation and i thought it was a nice opportunity
So, I am very confused over a quick test that I just ran. I
Is it just to test compile a simple program, with that header file #included
I have a trivial console application in .NET. It's just a test part of

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.