I have to frequently check the memory usage by an application - it right does it every 60 seconds using java.lang.Runtime.freeMemory()/java.lang.Runtime.total开发者_如何学CMemory()
What if I do the above say every 5 seconds - any performance implications?
(Hopefully not like System.gc() has)
The application runs on Linux/Solaris/Windows/HP-UX/AIX etc
Test it and see. If you can't construct a test that causes a meaningful drop in performance metrics than I guarantee you your users won't notice it either.
Gut feeling: shouldn't be a problem. Now whether an application that constantly monitors its memory has a good design...that's another question.
I tracked it down in openjdk jvm.cpp
JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void))
JVMWrapper("JVM_TotalMemory");
size_t n = Universe::heap()->capacity();
return convert_size_t_to_jlong(n);
JVM_END
searching for the implementation of Universe lead to
public CollectedHeap heap() {
try {
return (CollectedHeap) heapConstructor.instantiateWrapperFor(collectedHeapField.getValue());
} catch (WrongTypeException e) {
return new CollectedHeap(collectedHeapField.getValue());
}
}
Oops CollectedHeap is an interface ... thus I came to the conclusion that mark-peters 'test and see' would be best.
精彩评论