Jump to content

I am currently working on a project that compares how different three different languages (Java, C++, Python) handle things memory usage wise. I currently know that the JVM will have a major impact on this. To do so, I am programming 5 programs in each language with the same logic(goal) in each of them. I know for a fact that the methods Java will take will be different because the JVM instantly does stuff(like randomize the randomizer for you) so no extra memory will be consumed. The problem I am currently running into is with C++.

With Java, I have currently programmed all 5 programs and each program has

 

Runtime rt = Runtime.getRuntime();

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

 

in the front(main) and

 

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

 

in the back of the runner(main) of the program. This works perfectly by showing how much memory was originally used and then how much was used after the program was run, so if you subtract them, it shows how much memory the program actually uses.

 

The problem now is C++. I need a few lines such as the code above to see how much memory was originally used and how much the program uses.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well, you don't understand how C++ works, correctly... 

 

Java and python both run on virtual machines, java gets compiled to its own bytecode that the JVM understands, python again is just directly interpretted by its VM. 

 

C++ however is a fully compiled language, every instruction is compiled to machine code specific to the hardware you are running it on.

Now, this makes reading the memory use easy, looking at the tasks currently used memory will give you the exact amount of memory currently in use by the process. 

Memory is C++ is allocated when needed and destroyed and released when not, if memory is not destroyed/released then you have a memory leak. This is why memory management in C++ is very very important.

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

Well, you don't understand how C++ works, correctly... 

 

Java and python both run on virtual machines, java gets compiled to its own bytecode that the JVM understands, python again is just directly interpretted by its VM. 

 

C++ however is a fully compiled language, every instruction is compiled to machine code specific to the hardware you are running it on.

Now, this makes reading the memory use easy, looking at the tasks currently used memory will give you the exact amount of memory currently in use by the process. 

Memory is C++ is allocated when needed and destroyed and released when not, if memory is not destroyed/released then you have a memory leak. This is why memory management in C++ is very very important.

Ok, this post didn't answer my question, it just said memory management in C++ is very very important and everything around the actual answer. "looking at the tasks currently used memory will give you the exact amount of memory currently in use by the process" is kind of unclear since you can't just look at the tasks. What I want are lines of code to make it output how much memory the program and the IDE uses before running the program.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, this post didn't answer my question, it just said memory management in C++ is very very important and everything around the actual answer. "looking at the tasks currently used memory will give you the exact amount of memory currently in use by the process" is kind of unclear since you can't just look at the tasks. What I want are lines of code to make it output how much memory the program and the IDE uses before running the program.

It uses nothing, or should use nothing at least, the program will be spawned in its own thread, thats what you have to realise. The amount of memory in use should be exactly as portrayed. If the IDE is running it in some odd virtual environment then thats pretty damn odd. And in that case you should compile it for release and not run it inside the IDE. 

 

And no, there is no specific code to analyse the entire memory footprint, as it depends on the system currently being run on.

Arch Linux on Samsung 840 EVO 120GB: Startup finished in 1.334s (kernel) + 224ms (userspace) = 1.559s | U mad windoze..?

Link to comment
Share on other sites

Link to post
Share on other sites

It uses nothing, or should use nothing at least, the program will be spawned in its own thread, thats what you have to realise. The amount of memory in use should be exactly as portrayed. If the IDE is running it in some odd virtual environment then thats pretty damn odd. And in that case you should compile it for release and not run it inside the IDE. 

 

And no, there is no specific code to analyse the entire memory footprint, as it depends on the system currently being run on.

Well that just told me that the IDE I am using (Dev-C++) is just retarded one. I'll get back to you and mark this as solved if this works on NetBeans or something else

Link to comment
Share on other sites

Link to post
Share on other sites

Well that just told me that the IDE I am using (Dev-C++) is just retarded one. I'll get back to you and mark this as solved if this works on NetBeans or something else

Nope, still nothing. It literally doesn't show me anything. I can't find a tool to say how much memory is used, I compiled one of my programs and it showed nothing but a Done! and even ran it and got nothing.

Link to comment
Share on other sites

Link to post
Share on other sites

There's no global way to tell how much memory it uses because the program is executed by the compiler but doesn't run inside it, unlike java (and python?). The best way to tell is just to use task manager if you're trying to find out how much memory your program is using for debugging purposes. If you need to find it programatically, it depends whether you're running on windows, OSX, linux, or some embedded system because they all have a different way of getting the memory usage (api call).

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Check out this thread... I've already explained a bunch in there... and provided some useful links that should give you exactly what you are looking for...

 

I think the OP of the thread I just linked to is in your class! 0.o

 

Hope it helps...

Link to comment
Share on other sites

Link to post
Share on other sites

There's no global way to tell how much memory it uses because the program is executed by the compiler but doesn't run inside it, unlike java (and python?). The best way to tell is just to use task manager if you're trying to find out how much memory your program is using for debugging purposes. If you need to find it programatically, it depends whether you're running on windows, OSX, linux, or some embedded system because they all have a different way of getting the memory usage (api call).

I tried to used task manager first, but it is very inconsistent. I figured that it was possible somehow to see how much memory a program in C++ would use just like java and python (and pretty much all the other languages that I know).  And I am running windows. If anybody can just copy and paste some code or something, it would be so much easier.

Link to comment
Share on other sites

Link to post
Share on other sites

Check out this thread... I've already explained a bunch in there... and provided some useful links that should give you exactly what you are looking for...

 

I think the OP of the thread I just linked to is in your class! 0.o

 

Hope it helps...

He is. I already contacted him. No info so far that he has will help me too much either.

Link to comment
Share on other sites

Link to post
Share on other sites

I tried to used task manager first, but it is very inconsistent. I figured that it was possible somehow to see how much memory a program in C++ would use just like java and python (and pretty much all the other languages that I know).  And I am running windows. If anybody can just copy and paste some code or something, it would be so much easier.

 

I posted a link in the thread that described a c++ function that is in the POSIX library called getrusage() that you can use to grab the memory consumed at run time from a C++ program...

 

Check out that link...

 

Edit: Whoops just saw your post in the other thread and responded there also... so here's what I said in case you look here first:

 

 

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...

 

Hope that helps...

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

×