JVM memory consumption double of heapsize
I have set maximum heap size to 4gb for 64bit Weblogic JVM. When i stress-test app, heap size never go开发者_高级运维es over 4Gb, but java.exe in taskmanager can consume up to whopping 10 Gb. Where this consumption comes from?
Well, the whopping can have lot's of causes an none of them is especially exciting. First, as said before, in "task manager", have a look at the detailed info for the process and make sure your looking at the working set and not memory commit size (virtual memory).
Then, below is a list (from the top of my head) of things in the JVM process that won't count as java heap, others might be able to add to the list(or perhaps subtract or elaborate):
- Perm gen
- Thread stack space
- Shared code segments
- Process private code segments
- In process I/O buffers
- JVM "data/code" (hotspot stuff, various temporaries, caches, etc)
- Memory fragmentation in above mentioned data
But I agree with you, if you're actually looking at the working set, it sounds a bit whopping. We usually have 1-1.5 GB non java-heap memory usage on our 32-bit resin servers under high load (Debian Linux).
The total used / free memory of an program can be obtained in the program via java.lang.Runtime.getRuntime();
The runtime has several method which relates to the memory. The following coding example demonstrate its usage.
import java.util.ArrayList;
import java.util.List;
public class PerformanceTest {
private static final long MEGABYTE = 1024L * 1024L;
public static long bytesToMegabytes(long bytes) {
return bytes / MEGABYTE;
}
public static void main(String[] args) {
// I assume you will know how to create a object Person yourself...
List<Person> list = new ArrayList<Person>();
for (int i = 0; i <= 100000; i++) {
list.add(new Person("Jim", "Knopf"));
}
// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: "
+ bytesToMegabytes(memory));
}
}
精彩评论