如何在运行时监视某些java方法分配的内存 [英] How to moniter memory allocated by some java method at runtime

查看:176
本文介绍了如何在运行时监视某些java方法分配的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个java程序,我的类假设A有一些预定义的行为。但是用户可以覆盖我的班级以改变其行为。所以我的脚本会检查是否有一些子类,而不是我会调用它的行为,但如果他在代码中写了一些阻塞代码或内存泄漏会怎么样。

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.

这可能会造成伤害我的过程。在java中是否有任何方法可以监视某些方法分配的内存。

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

请建议。

推荐答案


但如果他在他的
代码中写了一些阻止代码或内存韭菜

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

对于阻塞代码部分,如果你有一些时间问题,你可以把方法的执行包起来说 Future 并让 ExecutorService 执行代码。这样,如果执行花费太多时间,你将能够取消执行。

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.

对于内存泄漏问题,我想你不是在谈论内存泄漏但是调用重写方法导致内存消耗增加。 java中的内存泄漏毕竟很少见。

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.

您将无法检测方法的内存消耗,而不是java的工作方式。记忆是全球性的。如果例如加载了外部库(JNI),或者类路径中的某个库现在将使用更多内存,您会怎么做?你无法分辨。

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

这篇关于如何在运行时监视某些java方法分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆