0

I am trying to get memory usage by my application. I followed this

Here is code that I write but after 1 GB it is giving negative values.

    _PROCESS_MEMORY_COUNTERS info;
    GetProcessMemoryInfo( GetCurrentProcess( ), &info, sizeof(info) );
    int membyte = ((size_t)info.WorkingSetSize);
    QString s1 = "MB";

    double mem = membyte/1048576.0;
    if(mem>900.0)
    {
        mem = mem/1024.0;
        s1 = "GB";
    }

    double f,i,t1,t2;
    f = modf (mem , &i);
    f = f*100;
    t1 = modf(f,&t2);
    mem = i + (t2/100.0);
Community
  • 1
  • 1
Tab
  • 785
  • 2
  • 11
  • 27

2 Answers2

3
int membyte = ((size_t)info.WorkingSetSize);

Don't do that. There's no guarantee the working set size will be in the range an int can represent. Perhaps you want to use an unsigned long or, better yet, a SIZE_T.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

Change

int membyte = ((size_t)info.WorkingSetSize);

to

SIZE_T membyte = info.WorkingSetSize;

The documentation says that the type of the WorkingSetSize member if SIZE_T. You casted it info the int type which is signed, whereas SIZE_T is unsigned.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115