Jump to content

C++ help needed

KingOFCruel

For a project for Ap comp sci, im checking how much memory a program that accomplishes the same task will use in each language, im compartiong python, Java, and C++. I need a built in function that tells me how much Memory in Kb/Mb each program uses, For the java program i used in netbeans:

 

Runtime rt = Runtime.getRuntime(); System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes");

 

 

 

 

The closest thing i came to list the process in GB,

PROCESS_MEMORY_COUNTERS_EX pmc; GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)); SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;

 

 

which would come out inaccurate if i did a conversion(it's like 0.1gb)

 

anyone know a function i can find to display memory used?

( i cant use memprofiler or any external tool btw, has to be program wise)

 

my IDE is dev C++

Building a PC: check it out here http://pcpartpicker.com/p/1qvon

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

No offense, but it doesn't make a whole lot of sense to compare memory usage in C++ and Java...

 

You have to guarantee that your implementation in one or the other is consistent for all data types... for primitives its easy... they are the same in both java and c++...

 

However, to help a bit more with your issue, you may find these useful:

Hope this helps!

Link to comment
Share on other sites

Link to post
Share on other sites

No offense, but it doesn't make a whole lot of sense to compare memory usage in C++ and Java...

 

 

It's a project for his advanced placement computer sciences class

Feel free to message me if you want to chat!

Link to comment
Share on other sites

Link to post
Share on other sites

No offense, but it doesn't make a whole lot of sense to compare memory usage in C++ and Java...

 

You have to guarantee that your implementation in one or the other is consistent for all data types... for primitives its easy... they are the same in both java and c++...

 

However, to help a bit more with your issue, you may find these useful:

Hope this

It not my my choice, its a project, any help? 

Building a PC: check it out here http://pcpartpicker.com/p/1qvon

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

It's a project for his advanced placement computer sciences class

 

You do realize AP computer science students should still know what basic data types are and how much memory they require right?

 

The memory usage in both of those languages at the most fundamental level will be exactly the same if the implementations are written as efficiently as possible... and even with Java's garbage collection, C++ still effectively handles its memory thanks to destructors...

 

My point is they are virtually the same memory management wise...

Link to comment
Share on other sites

Link to post
Share on other sites

It not my my choice, its a project, any help? 

 

Check out those links I added in my previous post... they talk about a couple different functions in both C++ and Python that do exactly what you asked about...

Link to comment
Share on other sites

Link to post
Share on other sites

Check out those links I added in my previous post... they talk about a couple different functions in both C++ and Python that do exactly what you asked about...

Thanks, as well as im basically measuring how each program is differently efficient with accomplishing a task.

Building a PC: check it out here http://pcpartpicker.com/p/1qvon

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks, as well as im basically measuring how each program is differently efficient with accomplishing a task.

 

Right I understand that, but what I'm saying is... its not the language that determines the efficiency of the memory management but the actual algorithms inside the code itself.

 

If you were to do the following:

 

  • Create an array, size 1024, of integers in C++, Java, C, etc... they would all require exactly the same amount of memory.
  • Then use a Quicksort algorithm in each language on their respective arrays of 1024 integers

You would find that the memory management may be the same or may be different when the quicksort is ran because the implementations that are built in for each language may be different...

 

One language could have it's standard sort implemented as an in place sort where no additional memory is used, another language could require another entire array being created or even multiple arrays...

 

If we did the quicksort recursively vs iteritively, we could use even more memory...

 

The problem here is that languages like C, C++, and Java aren't comparable in terms of memory management because when it comes down to the most fundamental level, they are all exactly the same...

 

And when you get into complex data types you can't guarantee that previously established code has implementations that are equally efficient in both languages because you didn't write them... and if you did or could... then they would be the exact same anyway...

Link to comment
Share on other sites

Link to post
Share on other sites

Right I understand that, but what I'm saying is... its not the language that determines the efficiency of the memory management but the actual algorithms inside the code itself.

 

If you were to do the following:

 

  • Create an array, size 1024, of integers in C++, Java, C, etc... they would all require exactly the same amount of memory.
  • Then use a Quicksort algorithm in each language on their respective arrays of 1024 integers

You would find that the memory management may be the same or may be different when the quicksort is ran because the implementations that are built in for each language may be different...

 

One language could have it's standard sort implemented as an in place sort where no additional memory is used, another language could require another entire array being created or even multiple arrays...

 

If we did the quicksort recursively vs iteritively, we could use even more memory...

 

The problem here is that languages like C, C++, and Java aren't comparable in terms of memory management because when it comes down to the most fundamental level, they are all exactly the same...

 

And when you get into complex data types you can't guarantee that previously established code has implementations that are equally efficient in both languages because you didn't write them... and if you did or could... then they would be the exact same anyway...

Thanks for the advice, and information i'll look into asking my teacher about changing the assignment

Building a PC: check it out here http://pcpartpicker.com/p/1qvon

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

The memory usage in both of those languages at the most fundamental level will be exactly the same if the implementations are written as efficiently as possible...

My point is they are virtually the same memory management wise...

These points sound a bit misleading to me. Just by nature of the fact that C++ compiles to native code and Java runs in a virtual machine will cause C++ to have a smaller memory footprint. Memory footprint is one of the most commonly acknowledged shortcomings of interpreted languages when compared to compiled ones. Yes, primitive data types will always have the same size. But the OP is asking about the whole program and not all data types are primitive. The key philosophical differences between all three of the languages name cause them to be implemented in very different ways which then causes them to have very different performance and memory characteristics. I.E. you could create compiled Java but that would break the "run anywhere" philosophy of java.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

Right I understand that, but what I'm saying is... its not the language that determines the efficiency of the memory management but the actual algorithms inside the code itself.

 

If you were to do the following:

 

  • Create an array, size 1024, of integers in C++, Java, C, etc... they would all require exactly the same amount of memory.
  • Then use a Quicksort algorithm in each language on their respective arrays of 1024 integers

You would find that the memory management may be the same or may be different when the quicksort is ran because the implementations that are built in for each language may be different...

 

One language could have it's standard sort implemented as an in place sort where no additional memory is used, another language could require another entire array being created or even multiple arrays...

 

If we did the quicksort recursively vs iteritively, we could use even more memory...

 

The problem here is that languages like C, C++, and Java aren't comparable in terms of memory management because when it comes down to the most fundamental level, they are all exactly the same...

 

And when you get into complex data types you can't guarantee that previously established code has implementations that are equally efficient in both languages because you didn't write them... and if you did or could... then they would be the exact same anyway...

here's the code incise you are curious:

 

#include <cstdlib> #include <iostream> #include "pdh.h" #include "TCHAR.h" using namespace std; class CountVowels { public: string sentence; int vowelA; int vowelE; int vowelO; int vowelI; int vowelU; CountVowels(string s) { sentence = s; vowelA = 0; vowelE = 0; vowelO = 0; vowelI = 0; vowelU = 0; } int getACount() { return vowelA; } int getECount() { return vowelE; } int getICount() { return vowelI; } int getOCount() { return vowelO; } int getUCount() { return vowelU; } void count() { //int number = 0; int length = sentence.length(); cout << "Length: " << length << endl; for(int i = 0; i < length; i++) { // char letter = sentence.substr(i,1); char letter = sentence.at(i); cout << "Letter: " << letter << endl; if(letter == 'A') { vowelA = vowelA + 1; } if(letter == 'E') { vowelE = vowelE + 1; } if(letter == 'I') { vowelI = vowelI + 1; } if(letter == 'O') { vowelO = vowelO + 1; } if(letter == 'U') { vowelU = vowelU + 1; } } } }; int main(int argc, char *argv[]) { string sentence = "KAEHONG"; // cin >> sentence; //cout << "Computer: Hi, " << sentence << "! You're a real swell person!" << endl; CountVowels letter(sentence); // letter.setValues(sentence); // letter(sentence); letter.count(); // return 0; cout << "Number of A: " << letter.getACount() << endl; static PDH_HQUERY cpuQuery; static PDH_HCOUNTER cpuTotal; void init(){ PdhOpenQuery(NULL, NULL, &cpuQuery); PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal); PdhCollectQueryData(cpuQuery); } double getCurrentValue(){ PDH_FMT_COUNTERVALUE counterVal; PdhCollectQueryData(cpuQuery); PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal); return counterVal.doubleValue; system("PAUSE"); return EXIT_SUCCESS; }

Building a PC: check it out here http://pcpartpicker.com/p/1qvon

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

These points sound a bit misleading to me. Just by nature of the fact that C++ compiles to native code and Java runs in a virtual machine will cause C++ to have a smaller memory footprint. Memory footprint is one of the most commonly acknowledged shortcomings of interpreted languages when compared to compiled ones. Yes, primitive data types will always have the same size. But the OP is asking about the whole program and not all data types are primitive. The key philosophical differences between all three of the languages name cause them to be implemented in very different ways which then causes them to have very different performance and memory characteristics. I.E. you could create compiled Java but that would break the "run anywhere" philosophy of java.

 

Ummm actually you aren't correct defining java as an interpreted language... Java is a static language and always requires compilation... Java also once compiled, has 98% of the speed of C.

 

You are not referring to the Java program itself being run but the overhead of the JVM, which is a virtual machine and not an interpreter... those two things are very different.

 

If you had read my previous posts I was talking about how you cannot compare complex data types unless they are implemented with the exact same efficiency in each language they are written... which would always be broken down anyway...

 

Through all of my posts I said nothing about Java and C++ being compared to Python... I was only referring to the comparison between Java and C++... because both are static languages, they will manage memory the same at the most fundamental level.

Link to comment
Share on other sites

Link to post
Share on other sites

here's the code incise you are curious:

 

#include <cstdlib> #include <iostream> #include "pdh.h" #include "TCHAR.h" using namespace std; class CountVowels { public: string sentence; int vowelA; int vowelE; int vowelO; int vowelI; int vowelU; CountVowels(string s) { sentence = s; vowelA = 0; vowelE = 0; vowelO = 0; vowelI = 0; vowelU = 0; } int getACount() { return vowelA; } int getECount() { return vowelE; } int getICount() { return vowelI; } int getOCount() { return vowelO; } int getUCount() { return vowelU; } void count() { //int number = 0; int length = sentence.length(); cout << "Length: " << length << endl; for(int i = 0; i < length; i++) { // char letter = sentence.substr(i,1); char letter = sentence.at(i); cout << "Letter: " << letter << endl; if(letter == 'A') { vowelA = vowelA + 1; } if(letter == 'E') { vowelE = vowelE + 1; } if(letter == 'I') { vowelI = vowelI + 1; } if(letter == 'O') { vowelO = vowelO + 1; } if(letter == 'U') { vowelU = vowelU + 1; } } } }; int main(int argc, char *argv[]) { string sentence = "KAEHONG"; // cin >> sentence; //cout << "Computer: Hi, " << sentence << "! You're a real swell person!" << endl; CountVowels letter(sentence); // letter.setValues(sentence); // letter(sentence); letter.count(); // return 0; cout << "Number of A: " << letter.getACount() << endl; static PDH_HQUERY cpuQuery; static PDH_HCOUNTER cpuTotal; void init(){ PdhOpenQuery(NULL, NULL, &cpuQuery); PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal); PdhCollectQueryData(cpuQuery); } double getCurrentValue(){ PDH_FMT_COUNTERVALUE counterVal; PdhCollectQueryData(cpuQuery); PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal); return counterVal.doubleValue; system("PAUSE"); return EXIT_SUCCESS; }

 

Oh god the formatting...

Link to comment
Share on other sites

Link to post
Share on other sites

Ummm actually you aren't correct defining java as an interpreted language... Java is a static language and always requires compilation... Java also once compiled, has 98% of the speed of C.

 

You are not referring to the Java program itself being run but the overhead of the JVM, which is a virtual machine and not an interpreter... those two things are very different.

 

If you had read my previous posts I was talking about how you cannot compare complex data types unless they are implemented with the exact same efficiency in each language they are written... which would always be broken down anyway...

 

Through all of my posts I said nothing about Java and C++ being compared to Python... I was only referring to the comparison between Java and C++... because both are static languages, they will manage memory the same at the most fundamental level.

Alright, I was just rolling the JVM overhead in with the program. Would that be wrong to do? And I brought up  the complex data type efficiency because there are complex data types that you will call from an internal library instead of hand coding. I thought it would be a bit unreasonable to compare hand built code where few would consider hand building it themselves. I understand that functionally identical code should work the same but java still doesn't compile to machine code and as such should have some form of inefficiency. Plus I thought Java implemented memory with an internal stack whereas C++ you did it manually (for better or worse). And I only mentioned Python because the OP did.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

Alright, I was just rolling the JVM overhead in with the program. Would that be wrong to do? And I brought up  the complex data type efficiency because there are complex data types that you will call from an internal library instead of hand coding. I thought it would be a bit unreasonable to compare hand built code where few would consider hand building it themselves. I understand that functionally identical code should work the same but java still doesn't compile to machine code and as such should have some form of inefficiency. Plus I thought Java implemented memory with an internal stack whereas C++ you did it manually (for better or worse). And I only mentioned Python because the OP did.

 

The complex data types from libraries are the ones I was referring to that are uncomparable because they may or may not have similar implementations between multiple languages, if Java has a quicksort function written using an in place algorithm and C++ uses a recursive algorithm where arrays are being re-created with each step... C++ would clearly be worse, so you can't compare code that isn't similar between both languages... because then there is no constant for comparison.  Also, the implementations for memory management in Java and C++ are not quite like you said... you don't necessarily manage all memory in C++ manually as C++ has something called destructors which cleans up memory from their respective complex data types much like Java's garbage collection.  But even still, because these happen so infrequently in code the comparable memory overhead is all defined by the algorithms implemented in code...  For example cleaning up an array of size 100K in both Java and C++ will be virtually the same give or take a couple bytes of data... but if you look at those couple of bytes in comparison to the ~800,000 bytes for a basic 100K size array of integers in C++ and Java... there really is not much of a difference.

 

Also, just to clear something up... the memory the OP was talking about is as a result of the program at run time and not as a result of any further overhead like the JVM (which isn't even much anyway...)

 

My whole point is that the memory usage in C++ and Java being static languages is so similar it is really useless to compare the two...

 

However, you can feel free to throw all of this out the window when comparing Java, C, C++ to dynamic languages like Python, Ruby, and Perl...

Link to comment
Share on other sites

Link to post
Share on other sites

The complex data types from libraries are the ones I was referring to that are uncomparable because they may or may not have similar implementations between multiple languages, if Java has a quicksort function written using an in place algorithm and C++ uses a recursive algorithm where arrays are being re-created with each step... C++ would clearly be worse, so you can't compare code that isn't similar between both languages... because then there is no constant for comparison.  Also, the implementations for memory management in Java and C++ are not quite like you said... you don't necessarily manage all memory in C++ manually as C++ has something called destructors which cleans up memory from their respective complex data types much like Java's garbage collection.  But even still, because these happen so infrequently in code the comparable memory overhead is all defined by the algorithms implemented in code...  For example cleaning up an array of size 100K in both Java and C++ will be virtually the same give or take a couple bytes of data... but if you look at those couple of bytes in comparison to the ~800,000 bytes for a basic 100K size array of integers in C++ and Java... there really is not much of a difference.

 

Also, just to clear something up... the memory the OP was talking about is as a result of the program at run time and not as a result of any further overhead like the JVM (which isn't even much anyway...)

 

My whole point is that the memory usage in C++ and Java being static languages is so similar it is really useless to compare the two...

 

However, you can feel free to throw all of this out the window when comparing Java, C, C++ to dynamic languages like Python, Ruby, and Perl...

Okay. I did learn both C++ and Java initiall nearly 10 years ago. I've largely kept up my C/C++ but I haven't touched Java. It sounds like my understanding of Java is severely out dated. 

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

Okay. I did learn both C++ and Java initiall nearly 10 years ago. I've largely kept up my C/C++ but I haven't touched Java. It sounds like my understanding of Java is severely out dated. 

 

Yeah writing Java is a large part of my career.

 

Actually, I kinda just write a crapload of code...

 

I probably know 30+ programming languages, I even know falcon! (it sucks... don't waste your time learning that one...)

 

My favorite is probably plain old C though...

 

Edit: boy this thread got kinda derailed quick

Link to comment
Share on other sites

Link to post
Share on other sites

Edit: boy this thread got kinda derailed quick

Sorry :P.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
Share on other sites

Link to post
Share on other sites

No offense, but it doesn't make a whole lot of sense to compare memory usage in C++ and Java...

 

You have to guarantee that your implementation in one or the other is consistent for all data types... for primitives its easy... they are the same in both java and c++...

 

However, to help a bit more with your issue, you may find these useful:

Hope this helps!

Dang it, do you have anything for C++ in Windows. The things on C++ shortcut is only for Linux and doesn't work on Windows. (I tried)

Link to comment
Share on other sites

Link to post
Share on other sites

Dang it, do you have anything for C++ in Windows. The things on C++ shortcut is only for Linux and doesn't work on Windows. (I tried)

 

Ahhh I see your issue now, I'd suggest creating a virtual machine to run Ubuntu or CentOS or something and try it in there...

 

VirtualBox is a free program to run Virtual Machines, and both Ubuntu and CentOS are free open source linux operating systems...

Link to comment
Share on other sites

Link to post
Share on other sites

Dang it, do you have anything for C++ in Windows. The things on C++ shortcut is only for Linux and doesn't work on Windows. (I tried)

Well in theory the following code should work

PROCESS_MEMORY_COUNTERS_EX pmc;GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));SIZE_T memoryUsage = pmc.PrivateUsage;

Although KingOFCruel says it didn't work correctly for him.

The thing is it should give the correct amount the program is physically using....based on MSDN documentation "Commit Charge is the total amount of memory that the memory manager has committed for a running process."

I would say try that and then let us know if it doesn't match up correctly...and how it doesn't (It should be noted Task Manager is not always correct in Windows, so if you base it off of that number it might not match :P)

 

To address KingOFCruel's original post...I would be a bit concerned about the Runtime rt = Runtime.getRuntime(); System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes")  line...just from quickly reading totalMemory is available to the VM and freeMemory is the amount of memory the VM has left.  I could be wrong in thinking but if 2 java programs run at the same time, would they run in the same virtual machine or discrete VM's...again not sure, so maybe someone could chime in on this.  If java programs don't run in discrete VM's then totalMemory-freeMemory might not be the best way to measure.

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Well in theory the following code should work

PROCESS_MEMORY_COUNTERS_EX pmc;GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));SIZE_T memoryUsage = pmc.PrivateUsage;

Although KingOFCruel says it didn't work correctly for him.

The thing is it should give the correct amount the program is physically using....based on MSDN documentation "Commit Charge is the total amount of memory that the memory manager has committed for a running process."

I would say try that and then let us know if it doesn't match up correctly...and how it doesn't (It should be noted Task Manager is not always correct in Windows, so if you base it off of that number it might not match :P)

 

To address KingOFCruel's original post...I would be a bit concerned about the Runtime rt = Runtime.getRuntime(); System.out.println("Used Memory: " + (rt.totalMemory() - rt.freeMemory())/1024 + " KBytes")  line...just from quickly reading totalMemory is available to the VM and freeMemory is the amount of memory the VM has left.  I could be wrong in thinking but if 2 java programs run at the same time, would they run in the same virtual machine or discrete VM's...again not sure, so maybe someone could chime in on this.  If java programs don't run in discrete VM's then totalMemory-freeMemory might not be the best way to measure.

 

I totally didn't even read that line before but you are entirely right... even without taking into account how the JVM manages multiple running programs, there is also a strong possibility that there is other things using memory in that runtime besides the actual process itself (especially because of some of the sketchiness of garbage collection) so totalMemory-freeMemory would potentially include memory not used by the running program... I'd be looking into another method of calculating the runtime memory usage for Java also...

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×