Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to moniter memory allocated by some java method at runtime

I am creating a java program in which my class suppose A has it's some predefined behavior. But user can over-ride my class to change its behavior. So my script will check if there is some subclass than I will call it's behavior but what if he has written some blocking code or memory leak in his code.

This may harm my process. Is there is any way in java to monitor memory allocated by some method.

Please suggest.

like image 544
Saurav Avatar asked Dec 05 '25 10:12

Saurav


1 Answers

but what if he has written some blocking code or memory leek in his code

First of all i suggest you document your class well. Describe what the user is allowed to do and what not. Give use cases what to do(if possible).

For the blocking code part, if you have some timing issues, you could wrap the execution of the method in say a Future and let a ExecutorService execute the code. That way you will be able to cancel the execution if the execution takes too much time.

For the memory leak issue, well i guess you are not talking about memory leaks but increased memory consumption caused by calling the overridden method. Memory leaks in java are rare after all.

You will not be able to detect the memory consumption of a method, that's not how java works. Memory is global. What will you do if for example an external library is loaded(JNI), or some library in the classpath is called that will use more memory now? You just can not tell.

Other then monitoring the overall memory consumption, there is no other way(someone please tell me if i am wrong).

like image 85
Ortwin Angermeier Avatar answered Dec 06 '25 23:12

Ortwin Angermeier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!