Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi All, I need a script to increase memory consumption for a 4 blade system. The memory consumption should be increase to suppose 60%, 70% like that. Any one have ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2012
    Posts
    1

    Script to increase memory consumption


    Hi All,

    I need a script to increase memory consumption for a 4 blade system. The memory consumption should be increase to suppose 60%, 70% like that. Any one have the script or someone will guide to write it?

    Thanks
    Sam

  2. #2
    Linux Newbie Syndacate's Avatar
    Join Date
    May 2012
    Location
    Hell..no literally, this state is hell..
    Posts
    192
    Quote Originally Posted by sameernagar View Post
    Hi All,

    I need a script to increase memory consumption for a 4 blade system. The memory consumption should be increase to suppose 60%, 70% like that. Any one have the script or someone will guide to write it?

    Thanks
    Sam
    If you just want to use a lot of memory I'd say the simplest way is to write a C program to leak it.

    I've done this before, but on some systems it doesn't work due to virtual memory playing games with it. Like I ran it on my OS X laptop and it happily consumed 1.1TB of memory when I told it to. Not quite sure how it believes this, only a 320GB HDD, so it didn't swap it. So yeah, somebody would have to do it with a more in-depth understanding than I.

    Sorry I can't help further.

  3. #3
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Hello and welcome!

    I've moved your question to the Programming/Scripting forum, where it will hopefully get you the best attention.

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    Quote Originally Posted by sameernagar View Post
    Any one have the script or someone will guide to write it?
    I came across a suggestion to a problem very similar to yours at this thread (3rd answer down). It is a small C prog, compiled cleanly and ran just fine on my system.

  5. #5
    Just Joined!
    Join Date
    Dec 2008
    Location
    Lund, Sweden
    Posts
    31
    Many systems including Linux have an overcommitting feature, whereby memory requested by malloc is not actually reserved until the memory is actually used. So just calling malloc(10000000) does not actually consume 10 megabytes of memory. What you need to do is write something to the memory; as memory is handled in pages of 4 kbytes (on most systems), you only need to write a single byte per 4 kbytes to actually use the memory (which saves time compared to writing to each byte you've allocated).

    So, something like:

    int size = 10000000;
    int p = malloc(size);
    int idx = 0;

    while (idx < size) {
    p[idx] = 0; /* or some other arbitrary value */
    idx += 4096; /* page size */
    }

    If you do something like this on a system which has too little memory compared to the size you are trying to allocate, you'll find the program terminating with an out of memory error in the while loop.

  6. #6
    Linux Newbie Syndacate's Avatar
    Join Date
    May 2012
    Location
    Hell..no literally, this state is hell..
    Posts
    192
    Quote Originally Posted by ricard View Post
    Many systems including Linux have an overcommitting feature, whereby memory requested by malloc is not actually reserved until the memory is actually used. So just calling malloc(10000000) does not actually consume 10 megabytes of memory. What you need to do is write something to the memory; as memory is handled in pages of 4 kbytes (on most systems), you only need to write a single byte per 4 kbytes to actually use the memory (which saves time compared to writing to each byte you've allocated).

    So, something like:

    int size = 10000000;
    int p = malloc(size);
    int idx = 0;

    while (idx < size) {
    p[idx] = 0; /* or some other arbitrary value */
    idx += 4096; /* page size */
    }

    If you do something like this on a system which has too little memory compared to the size you are trying to allocate, you'll find the program terminating with an out of memory error in the while loop.

    Thanks, always wondered why mine didn't work. Never asked, though! That fixed it just fine.

    @ OP:
    This code works (I just tested it on OS X) for holding the memory you tell it to. It should work for your purposes. It only uses the std C libs.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
        float input = 0;
        printf("Enter amount of GB to use (Decimals accepted):\n");
        scanf("%f", &input);
    
        int num_chunks = (input * 1024.0f * 1024.0f) + 1;
        void *waste = 0;
        printf("%d KB chunks to allocate...\n", num_chunks);
    
        int mem[256];
        for (; num_chunks >= 0 ; num_chunks--)
        {
            waste = malloc(1024);
            memcpy(waste, mem, 1024);
        }
    
        printf("%0.2fGB of memory allocated, 'enter' to release memory and exit\n", input);
    
        getchar(); // Flush the newline from scanf
        char c = 0;
        while (c != '\n')
            c = getchar();
    
        return 0;
    }
    EDIT:
    You can just copy/paste it into a file with the extension of '.c' and compile with:
    Code:
    gcc file_name.c
    And run via:
    Code:
    ./a.out

  7. #7
    Just Joined!
    Join Date
    Dec 2008
    Location
    Lund, Sweden
    Posts
    31
    Actually, most systems have automatic make, so once you have your C file you should be able to do

    $ make file_name

    which will then create the 'file_name' executable from file_name.c using whatever compiler you have.

  8. #8
    Linux Newbie Syndacate's Avatar
    Join Date
    May 2012
    Location
    Hell..no literally, this state is hell..
    Posts
    192
    Quote Originally Posted by ricard View Post
    Actually, most systems have automatic make, so once you have your C file you should be able to do

    $ make file_name

    which will then create the 'file_name' executable from file_name.c using whatever compiler you have.
    Since there's no external libs to link against or anything it's actually more letters to type to use automake than it is compile it manually .

  9. #9
    Just Joined!
    Join Date
    Dec 2008
    Location
    Lund, Sweden
    Posts
    31
    Well, not really, "gcc file_name.c" is one more character than "make file_name". Plus, there's the added bonus of not having any repeated characters (i.e. 'cc'), which are slower to type.

    Of course, the executable 'a.out' is shorter to type than 'filename', but you can use tab completion for the latter (assuming you have a shell with that feature) anyway. And, perhaps most importantly, a.out is not a descriptive file name, a few days and some more C files later you have no idea what your a.out is, whereas an executable with the same base name as the C file will be immediately obvious.

  10. #10
    Linux Newbie Syndacate's Avatar
    Join Date
    May 2012
    Location
    Hell..no literally, this state is hell..
    Posts
    192
    Quote Originally Posted by ricard View Post
    Well, not really, "gcc file_name.c" is one more character than "make file_name". Plus, there's the added bonus of not having any repeated characters (i.e. 'cc'), which are slower to type.

    Of course, the executable 'a.out' is shorter to type than 'filename', but you can use tab completion for the latter (assuming you have a shell with that feature) anyway. And, perhaps most importantly, a.out is not a descriptive file name, a few days and some more C files later you have no idea what your a.out is, whereas an executable with the same base name as the C file will be immediately obvious.
    Yeah, I thought about that, but figured it'd be easier if the 'line' to type to compile was less complex. So I opted to leave out an output flag/file name.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •