I have two Java programs. On my computer, one of them uses 9MB of RAM and the other uses 77MB. But when I upload them to a server, the same programs use 382MB and 186MB! Is there a way to stop this from happening?
-
3Are you talking about physical memory or virtual memory? If virtual memory -- who cares? Virtual memory is not a scarce resource. – David Schwartz Nov 03 '11 at 22:25
-
1Did you try taking a memory dump? You can see your memory utilization using - http://java.sun.com/developer/technicalArticles/Programming/HPROF.html – Pushkar Nov 03 '11 at 22:28
-
3Are you sure its only using 9 MB total, I find that hard to believe? On a machine with more memory the maximum memory is increased. This means more virtual memory is used but not more resident memory. (This is the figure you should look at) – Peter Lawrey Nov 03 '11 at 22:32
4 Answers
How do you measure the memory usage in each case? Different operating systems have a different concept of what constitutes "memory usage".
64-bit systems require more memory than 32-bit systems due to the increased pointer (reference in Java speak) size. Are you using a 32-bit OS and JVM on your desktop computer?
Are you using different JVM options? A moderately active Java application usually ends up using all the memory that is permitted by the
-Xmx
option, to minimize the CPU time spent on garbage collection. In addition, the default maximum heap space is determined in relation to the available physical memory - if the server has more memory, the Java applications are bound to use more memory as well.Server JVMs (see the
-server
JVM option) have different settings and favor performance over memory usage. The-server
option is the default on 64-bit systems.Are you absolutely certain that the application load is the same in both cases?

- 84,049
- 23
- 157
- 201
It is quite common for applications to allocate virtual memory in large chunks to improve performance and efficiency. Nobody bothers to optimize such things because they have no effect. If you don't actually have a problem, there's nothing to fix.
Virtual memory is not a scarce resource. Attempting to reduce the consumption of vm is wasted effort.

- 179,497
- 17
- 214
- 278
-
it depends on whether you have enough disk space - if you server has limited resources, may be it is worthwhile to reduce it a bit. – Chii Nov 03 '11 at 22:29
-
1@Chii: I don't understand the connection at all. Virtual memory that never maps to resident pages doesn't consume disk space. – David Schwartz Nov 03 '11 at 22:31
-
1@Chii No, really, virtual memory is just numbers. There are plenty of numbers. A server may have limits on other resources, but it can have as many numbers as it wants. – Pascal Cuoq Nov 03 '11 at 22:33
-
@Chii: I think in this case we have a terminology ambiguity. Depending on the context, "virtual memory" can mean both the use of secondary storage for use by the OS memory manager (swap) and the mapping of disk files into the address space of a process. The fact that both use the same CPU facilities does not help either... – thkala Nov 03 '11 at 22:46
-
1@thkala: The use of the term "virtual memory" to refer to swapping or paging is, IMO, a misuse of the terminology. In fact, there are systems that have virtual memory and no swapping and there are systems that swap and have no virtual memory. – David Schwartz Nov 03 '11 at 23:28
-
@David Schwartz: I agree, but that does not make the situation any less confusing :-/ – thkala Nov 03 '11 at 23:37
-
@thkala: At least in my experience, the confusion of virtual memory with paging appears to be limited to the Windows context. Since this question is about Java on Debian, I would hope we can assume that [virtual memory](http://en.wikipedia.org/wiki/Virtual_memory) is understood to have its usual meaning. But you're probably right that at least one person got it confused. – David Schwartz Nov 03 '11 at 23:58
How did you measure that numbers?
Comparing the numbers of Windows Task Manager and ps(1) on Linux is futile because they are computed differently. For example shared libraries and shared memory are attributed differently on both platforms. Also the memory management is completely different.
If, on the other hand, you refer to numbers gathered from within your apps via Runtime
(or similar), then you have to look how the JVM was started with what parameters. Most important are the parameters -Xmx
and -Xms
but you might lookup several others in the doc of either java
or javaw
.
Related to point 1:
How to measure actual memory usage of an application or process?
Unless you've explicitly set it (e.g command line arguments like -Xmx128M), the default maximum heap size of the JVM depends on the amount of RAM available.

- 24,582
- 12
- 83
- 135