Arpssss
Arpssss

Reputation: 3858

How to bound JVM with a fixed memory limit?

I want to bound JVM with a fixed memory limit. Can any one help me how to do this ?

More specifically, I want that my code can use max 1024 mb memory space and my JVM has to initially block that much of memory.

Another thing, I have 4 GB memory but my JVM shows only 878 MB memory. Why ?

I got it using: Runtime runtime = Runtime.getRuntime() and runtime.totalMemory()/(1024*1024)

Why C has not this kind of issue ?

Upvotes: 0

Views: 282

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533870

Java won't use more memory than it needs, if you have a hello world program, it won't use 1 GB no matter what you set the maximum to.

In C, memory is either used, or freed. There is not other state for memory. In Java, you have memory which has a string reference, a soft reference, a weak reference and no reference. The maximum memory you determines when to clean up memory and whether soft/weak refernces get cleaned up.

The problem with C memory is you have to know when memory is no longer used, which can result in memory leaks and/or additional complexity which is exposed to the developer. Java doesn't have these problems as it has managed memory.

C programs also have the problem they can grow until you run out of memory+swap. For a well behaved program this is not a problem, but its another thing the developer needs to worry about because he doesn't know how much memory the machine the program might be run on might have.

Upvotes: 1

ControlAltDel
ControlAltDel

Reputation: 35106

you can run 'java' with the following options

-xms minimum memory size -xxs max memory size

Upvotes: 0

Related Questions