I am running top
to monitor my server performance and 2 of my java processes show virtual memory of up to 800MB-1GB. Is that a bad thing?
What does virtual memory mean?
And oh btw, I have swap of 1GB and it shows 0% used. So I am confused.
Java process = 1 Tomcat server + my own java daemon Server = Ubuntu 9.10 (karmic)
Virtual memory isn't even necessarily memory. For example, if a process memory-maps a large file, the file is actually stored on disk, but it still takes up "address space" in the process.
Address space (ie. virtual memory in the process list) doesn't cost anything; it's not real. What's real is the RSS (RES) column, which is resident memory. That's how much of your actual memory a process is occupying.
But even that isn't the whole answer. If a process calls fork(), it splits into two parts, and both of them initially share all their RSS. So even if RSS was initially 1 GB, the result after forking would be two processes, each with an RSS of 1 GB, but you'd still only be using 1 GB of memory.
Confused yet? Here's what you really need to know: use the
free
command and check the results before and after starting your program (on the+/- buffers/cache
line). That difference is how much new memory your newly-started program used.I found this explanation from Mugurel Sumanariu very clear:
From the top(1) man page:
(Prior versions of the documentation stated "VIRT = SWAP + RES." Where RES means RESident, or physicical memory used.)
Actually that's not correct (anymore). When it says "swap," that also includes files that the program has mapped into its address space, which may or may not actually be consuming real RAM yet. This memory is file-backed but isn't really swap.
VIRT also includes pages that have been allocated but not used for anything yet. Any page in this state is mapped to the kernel Zero Page (brilliant concept--you should look it up) so it shows up in VIRT but doesn't actually consume any memory.
VIRT column in the ps/top output is almost irrelevant to measure memory usage. Don't worry about it. Apache heavy load VIRT vs RES memory
https://stackoverflow.com/questions/561245/virtual-memory-usage-from-java-under-linux-too-much-memory-used
VIRtual
column of the top, refers to the super-space (super consumption space) of the process, which the process might not be actually taking at the run time. There is another columnRESident
, which refers to the actual physical memory/space allocated by the process, at the runtime.The reason for difference, between the two, can be understood by the example: if the process is using certain library, then the library size, will also aid to the
virtual-size
. however, since only a part of the library would be used (i.e. some methods in use), so that will aid inresident-size
.Refer for More Info
"VIRT" just address space, RES is the "real" memory, but the "SHR" (=shared) amount of "RES" is the part of RES that is shared with other processes. So for most processes, I believe that by subtracting SHR from RES gives you the amount of memory that is really attributable to this particular process.
Linux supports virtual memory, that is, using a disk as an extension of RAM so that the effective size of usable memory grows correspondingly. The kernel will write the contents of a currently unused block of memory to the hard disk so that the memory can be used for another purpose. When the original contents are needed again, they are read back into memory. This is all made completely transparent to the user; programs running under Linux only see the larger amount of memory available and don't notice that parts of them reside on the disk from time to time. Of course, reading and writing the hard disk is slower (on the order of a thousand times slower) than using real memory, so the programs don't run as fast. The part of the hard disk that is used as virtual memory is called the swap space.
Linux can use either a normal file in the filesystem or a separate partition for swap space. A swap partition is faster, but it is easier to change the size of a swap file (there's no need to repartition the whole hard disk, and possibly install everything from scratch). When you know how much swap space you need, you should go for a swap partition, but if you are uncertain, you can use a swap file first, use the system for a while so that you can get a feel for how much swap you need, and then make a swap partition when you're confident about its size.
You should also know that Linux allows one to use several swap partitions and/or swap files at the same time. This means that if you only occasionally need an unusual amount of swap space, you can set up an extra swap file at such times, instead of keeping the whole amount allocated all the time.
A note on operating system terminology: computer science usually distinguishes between swapping (writing the whole process out to swap space) and paging (writing only fixed size parts, usually a few kilobytes, at a time). Paging is usually more efficient, and that's what Linux does, but traditional Linux terminology talks about swapping anyway.
Source: http://www.faqs.org/docs/linux_admin/x1752.html