ealeon
ealeon

Reputation: 12452

Python vs Java's -xms and -xmx

Related question: What are the Xms and Xmx parameters when starting JVMs?

Why do Java programs lets you specify -xms and -xmx? As a developer, why do I need to know/be concerned for how much cpu/mem my program will use upfront?

Or same question in different angle, why would not let Python specify these types of options? (worked in Python for 5 years and never had to do this)

Does it have to do with Python being interpreted language and Java being some sort of bytecode to JVM to machine language thing?
Or maybe something to do with how garbage collection is being done differently that Java needs to be more wary of memory consumption?

Upvotes: 2

Views: 9752

Answers (3)

Hari Rao
Hari Rao

Reputation: 3240

  1. I see that Python has three generations i.e Generation 0, 1 and 2 pretty much like (Not equivalent but some what similar) Java's young, old and perm gen.
  2. As we know the switches put a cap on a Java application's memory usage.
  3. Similarly in Python although I don't see an equivalent (or there could be..) but I see gc.set_threshold to set a threshold (https://docs.python.org/3/library/gc.html).
  4. May be the threshold can be set to fine tune there by leading to an acceptable GCs' (again frequent GCs' are not good but it all boils down to fine tuning by playing around in Dev env and getting the threshold suitable for once own application).

Just a thought!

Upvotes: 0

Sraw
Sraw

Reputation: 20224

I think this is a question about the design pattern of java. It is really strange as that java is mostly the only one which strictly limits its memory usage. And it is also mostly the only one which has checked exception.

I believe these things are all because that the designers of java wants to design a language which is really for enterprises. They are trying their best to make it safe.

Here is an answer about the limitation of memory usage: https://stackoverflow.com/a/3358383/5588279

And for checked exception, that's the same. Designers want to force developers to handle every expectable exceptions.

Well, it's hard to say if that's good. Although I don't like this design as it seems that the designers treat me like a noob, but I have to admit it really makes java somehow safer... Maybe?

Upvotes: 2

Sam
Sam

Reputation: 2969

Not all operating systems communicate memory "pressure" to your running process in the same way, meaning nearly-never. Java lets you put an explicit limit on its memory use. Python, Go, and others have a somewhat aggressive method of collecting memory.

This is complicated by cgroups, used to implement containers under Docker and Kubernetes, which may note cause malloc() to return NULL, but to cause a signal be sent to your process to kill it.

FWIW, the JVM gives you a means (by -Xmx) to control the virtual machine's memory usage where as other systems just hope they fit. Java in a Docker container is very manageable where as Python or Go is a bit more hit-or-miss. These are all "good" technologies, but limits on containers should (I would hope) push for improvements in in how garbage collection "effort" is expressed. :\

This feels like a non-answer, and for that I'm dissatisfied. You can read this until someone posts something folks like better and up-vote. I'll delete this once a few inevitable grump-votes show up. ;)

Upvotes: 5

Related Questions