Reputation: 9110
First of all, this is not an "off-line" profiling task!
I am working on some SCala
codebase, and currently what I am trying to do is, if a function foo
consumes too many memory (let's say over 10G), kill this function and return a default value.
So it should look like:
monitor{
foo() <--- if foo has used over 10G memory, just cut it off
}
catch {
case MemoryUsageError => default_value
}
Note that currently foo
is running in the same process with my main
function.
Is it possible to do so? I quickly googled such materials and only find a way to show the current memory usage of a SCala
application; it is not as fine-grained as what I am looking for.
Am I clear on this? Could anyone shed some lights here? Thanks a lot!
========================================================================
Note that what I am looking for is an "online" method! It is not like off-line profiling. My application ifself should determine the memory usage of foo
function, and if it goes too high, just cut it off.
Upvotes: 2
Views: 1339
Reputation: 1766
In general jvm doesn't track creator of objects allocated on heap and place of creation. This is very costly and doesn't matter for GC.
Future
with predetermined Thread
and interrupt it if needed or using ForkJoinTask
and cancel()
method.finalize()
!Upvotes: 0