Thread: The code for estimating-Memory usage

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    72

    The code for estimating-Memory usage

    Hi
    i want to learn how can i find the memory usage of my computer.
    For example, my ram is 512mb, when i play game, it sometimes fullfilled and my pc shut down otomaticly.I found a program which is called 'RamFree' and i interested in how can i write the program that it will free my ram and help to run games better.
    I thought that if find a code which shows %usage of memory,
    i can put a infinite loop, and i set a percentage ,for example


    Code:
    char *p;
    while(1)
    {
    //i may put here a timer( it will make the loop 1 second by 1 second)
    while(memory_percentage>0.95)  
    {
     // i will allocate some memory here
     calloc  *p   //for example if i need to free %10 i will calculate how many bytes should i write //in calloc function and after i will create my own code
    
    at the end i will free(p) 
    }
    }
    Could you help me please?
    i searched the google but always i see the memory allocation topics and etc.
    if you have any code about estimating %'ram using',please share with me.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > For example, my ram is 512mb, when i play game, it sometimes fullfilled and my pc shut down otomaticly
    And how do you know this is down to lack of RAM?

    > can i write the program that it will free my ram and help to run games better.
    It's doubtful that you could do any better than what the OS already does anyway.

    The amount of memory available (as seen by a normal userland process) is only very loosely coupled to the amount of real physical RAM you have. The OS reserves some for itself, and the rest is allocated between all the user processes, along with some clever use of swap files to give the illusion that there is more memory than there really is.

    Your best bet (assuming it's a memory problem) is to get some kind of process monitor, see which unwanted app is using the most memory, then just quit it before running the game.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    i searched a lot, i didnt find anything about how to find the used memory in megabytes in realtime.
    after i did this program.My real aim was, combinening all of options or programs in one exe.I think i did.I know a little vb and c
    i think it will be helpful for some other friends who doesn't know C++
    or who want to combine Vb and C in one platform.Actually it is only calling one another exe.Not advanced things.I didnt send all of my program, i only did a basic running C program in a vb button.
    hope to be helpful for you.
    all of them in zip.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The C code:

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    int main(){ 
    float *mem, count = 0; 
    printf("This program coded to Free-Ram operation by Tomax\n\n");
    printf("This operation may take aproximetly 10 seconds(related to your value)\n\n");
    
    while(1){
    printf("How many mb(max 200mb) do you want to free ram : ", count); 
    scanf("%f",&count);
    if(count<201) break;
    else printf("\nYou reached the limit please try again.(limit 200mb)\n");
    }
     mem =calloc((count*1024*1024), sizeof(char)); 
    
    free(mem); 
    printf("Succeed!\nRam %.0fmb freed.\n",count);
    return 0; 
    }
    I will withhold comment.

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Hah, I wonder how many others did the same, I did. You forgot to mention that it was created in VS6 and compiled in Debug mode (natch), probably because, well it's a debug tool innit.

    EDIT:
    Oh look, there's an actual C source file in the root. How the flood did I miss that? Oh well, at least IDA-ing it up confirms the included exes match the source. Not that anybody was going to run them anyway
    Last edited by adeyblue; 02-06-2012 at 05:43 PM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    @adeyblue
    actually , i wanted to make an exe , in exe.in the other words, i would like to create an exe, which contains another exes in main exe, but i didnt.After i tried to find where is exe and find-run operation.It worked.Also i used Visual basic 2006, i forgot to say, sorry.
    And well, you looked the c code, is this dangerous?
    for example if it free the windows processes and their memory content in Ram,
    may it crash the computer?

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Is it dangerous? No. Is it flawed and pointless? Absolutely. You are doing absolutely nothing worthwhile in this program other than wasting clock cycles, and certainly nothing approaching what you *think* you're accomplishing. You have a fundamental lack of understanding of processes and memory management. Your program asks the user for an amount of memory to allocate, allocates that amount, and then immediately deletes it and exits. All solely within the context of *your* program. It has absolutely no effect on the memory used by other programs.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    Quote Originally Posted by rags_to_riches View Post
    Is it dangerous? No. Is it flawed and pointless? Absolutely. You are doing absolutely nothing worthwhile in this program other than wasting clock cycles, and certainly nothing approaching what you *think* you're accomplishing. You have a fundamental lack of understanding of processes and memory management. Your program asks the user for an amount of memory to allocate, allocates that amount, and then immediately deletes it and exits. All solely within the context of *your* program. It has absolutely no effect on the memory used by other programs.
    No, it really works great.For example when i open the game, it use 300 mb memory,
    max memory is 512 i have.
    When i use my program, i can see in taskmgr, it is getting lower.The memory usage of game is decreases.So i can alt+tab and search on internet, otherwise my pc crash.'Not enough memory'. Also my VGA crash, because 64mb + share with ram.
    Think that there is a garbage memory, some programs using it, while i use this program, it steals the memory places of games etc. and makes them free.So there is enough ram memory to use in another programs.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    It probably "works" occasionally at best, out of sheer luck.

    Get a new computer that has more than 512 mb RAM, or expect a buggy system running on an outdated computer to fail.

    Learn a bit about how memory works. Your program does not "steal" memory from anything, if it did, then how would the game be exactly the same when you went to play again? You're just wasting memory, and probably making the game laggier. By the 200th cycle, you're wasting about 200 megabytes of RAM if it successfully allocates. Unfortunately, few systems will allow one process to allocate such a huge chunk of memory all at once, and because you aren't even checking to see if it's allocated, your program is probably not doing anything.

    Also, not probably relevant, but your code is a nightmare. Learn to indent it properly, and give better user output. "This operation" should not take ten seconds, seriously, it should not even be a second. You don't check to see if calloc() failed, and judging from your extreme misuse of the function (why sizeof(char) in # of elements?), it seems as if you should do some research.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Usage
    By MK27 in forum Linux Programming
    Replies: 2
    Last Post: 07-16-2009, 05:52 PM
  2. Memory usage and memory leaks
    By vsanandan in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 05:45 AM
  3. estimating time remaining
    By Compengineer in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2003, 09:38 AM
  4. Memory Usage
    By ghe1 in forum Linux Programming
    Replies: 0
    Last Post: 03-18-2002, 09:43 AM
  5. Memory usage
    By Razor Ice in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2002, 03:16 PM