Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find memory used by a function in java

Tags:

java

I am calling a function from my code (written in java) and I want to know how much memory that function is using, and do keep in mind that I cannot add any code to the function(which I am calling).

for eg-

 //my code starts
   .
   .
   .
   .
   myfunc();
 //print memory used by myfunc() here
  .
  .
 // my code ends

How to do this?

like image 296
Rohit Elayathu Avatar asked Feb 20 '23 19:02

Rohit Elayathu


1 Answers

What you're trying to do is basically pointless. There is no such thing as memory used by a function. Your idea of comparing the total memory usage "before" and "after" function call does not have any sense: the function may change global state (which may decrease or increase total memory usage, and in some cases (e.g. cache filling) you probably won't want to consider the increase to count as "memory used by a function), or the garbage collector may run while you're inside of myfunc and decrease the total used memory.

The good question often contains the large part of an answer. What you should do is to correctly ask the question.

like image 79
penartur Avatar answered Mar 03 '23 11:03

penartur