I want to know how much memory is allocated in executing a method ..
Is there any pre-defined method to calculate the memory allocated or any process to calculate ...
Note : Method may contain any number of lines ...
For Example : To calculate time taken to execute method is calcluated by calling System.currentTimeMillis(); before and after calling the particular method . In this way is there any way to calculate memory required
Thank You ...
You can use a profiler.
They usually offer insights into the heap (and many other interesting things) so you can compare allocations before and after executing a method, which also is the standard approach to finding memory leaks, besides static analysis.
More advanced tools give you more detailed insights and analysis options.
For starters, take a look at visualvm, which comes for free with all current JDKs from Oracle.
You could use Runtime.totalMemory() which (per the Javadoc) returns the total amount of memory in the Java virtual machine.
System.out.println(Runtime.getRuntime().totalMemory());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With