3

For the following no-op code,

public class a {
public static void main(String args[]) throws Exception{
Thread.sleep(100000);
}
}

If I run it on a 64-bit jvm, through "top" I can see that it uses 2GB virtual memory. What is taking up that virtual memory? This example may be weird, but we do see some production code that uses a lot of virtual mem so that it exceeds ulimit -v

Thanks Yang

sonu
  • 95
  • 2
  • 7
teddy teddy
  • 3,025
  • 6
  • 31
  • 48
  • 1
    See [this question](http://stackoverflow.com/questions/561245/virtual-memory-usage-from-java-under-linux-too-much-memory-used). Virtual memory consumption is pretty much meaningless. – ig0774 Jan 11 '12 at 00:29

2 Answers2

4

Virtual memory does not mean that it is actually allocated and being used. It simply means it has that much currently addressable for use if need be.

Ben S
  • 68,394
  • 30
  • 171
  • 212
  • I know, but for what has it mapped in ?? the binary itself plus the libraries are not that big. – teddy teddy Jan 11 '12 at 00:38
  • "doesn't not" - I'm pretty sure you didn't intend the double negative here. I'd fix it myself but I'm not quite to 2k rep yet – Kevin K Jan 11 '12 at 00:58
  • @user933882 It's just mapped in case it's necessary. On a 64-bit machine, the virtual address space is huge, so it doesn't really matter if a 2GB block gets mapped to a process. – Ben S Jan 11 '12 at 01:01
  • ok I found this discussion http://stackoverflow.com/questions/1477885/trying-to-locate-a-leak-what-does-anon-mean-for-pmap pmap shows: `00000000901d0000 1195584 rw--- 0000000000000000 000:00000 [ anon ] 00000000d9160000 39808 rw--- 0000000000000000 000:00000 [ anon ] ` so looks just to be a JVM implementation behavior. – teddy teddy Jan 11 '12 at 01:20
  • http://oreilly.com/catalog/spt2/chapter/ch04.html#79188 Heap space Consists of memory allocated by means of malloc(3). This is called anonymous memory, because it has no mapping in the filesystem (we'll discuss it further in "Anonymous memory" later in this chapter). – teddy teddy Jan 11 '12 at 01:22
0

When you start a Java application it creates its heap (to its maximum size) on start up. The default size for recent Sun/Oracle JVMs is 1/4 of your main memory. This sounds wasteful, except all it does is reserve address space. Given each application has its own address space, this doesn't matter much. (unless you have a 32-bit JVM with limited address space)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130