Java, JVM internals - java

I am running Eclipse, Tomcat, a servlet based webapplication my windows 10 machine. I was wondering how many JVM instances will be created?
Also, When will JVM be instantiated in windows 10 on startup or on starting any java based app.

One per process. See the Processes tab of your Task Manager.
When starting a JVM process (typically once per app, but could be more if it forks).

Related

Is there a Java equivalent to a windows service [duplicate]

This question already has answers here:
Run Java application at Windows startup
(8 answers)
Closed 6 years ago.
I know you can run a jar as a windows service by calling it from a batch file or something similar to that, and I know that when you use an application server such as WAS, or Glassfish you have JVMS's that are "running" per-se. But unless I am mistaken, those aren't necessarily "services". That is to say processes that are executing without input from any user. From my understanding you still need to call a JVM to get it to execute a process (and please feel free to correct me if I am wrong). But I have always been curious as to what is the Java/Linux equivalent of the Windows Service? Maybe it is JVM's running in an application server like Tomcat, or Glassfish.
A Windows Service is basically a background task that runs a process for you. In Windows, they are typically binary's (.exe's) that can hook into the Windows Service platform for startup and shutdown messages from the OS (and the services administration screen).
Anytime you execute Java, you need runtime component (JVM) to run it. Even Websphere and the other app servers are running in a JVM.
There isn't anything in Linux for you to hook into a "Service" per say, but you can emulate the functionality and run startup items in Linux at boot time.
It depends on the exact flavor of Linux being used, but the conceptual siblings to a Windows Service (i.e. a long-running daemon that the OS starts when the OS is started) are upstart, systemd, etc.

Java Application Servers and JVM

When you deploy many applications to a java application server, do those applications all run in the same JVM i.e. the JVM that's started when the application server starts up?
Do you have the option to run each of those applications in a separate JVM? If so why would you want to do this?
java application server runs in a single JVM, so every app deployed under java application server instance runs in the same VM as every other application while every app has a different class loader
Go through this questions's answer..hope all queries will be answered :
Why have one JVM per application?
I am afraid you can't run in different JVMs because the appserver have to manage the objects life cycle. That's what JEE is all about. Also, that's why JEE states that you should not use threads in your app, because you want the container to take care of the concurrency for you.
Of course, in a clustered environment, you can have several JVMs, but still be the same for the app server + container.
Yes if the application server is not clustered.
Otherwise it could work on different host machine and jvm.

Setting the multiple JREs in the same application

I have implemented one application using JDK 1.6 64-bit, JSF, Tomcat server 64-bit etc at user system.I am integrating some devices(i.e. finereader, scanner, etc) into this application. Some of devices libraries are supported only on JRE 32-bit Only.
So, device integrated screens to be run on JRE 32-bit and non device screens should be run on a JRE 64-bit. Can I configure multiple JRE's in the same application? If possible, how?
A Java application runs in a JVM. The JVM is part of one single JRE. You could split your application into two parts, which each run in their own JVM. These applications then would have to communicate with each other to coordinate the user experience. This does not look like a good solution to me.
It all depends on how you define "application."
First, in regard to your question, each JVM instance runs a single kind of Java--32 or 64 bit--Java version and such.
Sometimes, an application consists of a single JVM running a single java executable, usually a jar and some stuff on the classpath with a single 'main'
Sometimes, an application consists of multiple JVMs running on one or more boxes. In this case, each JVM is running a single java executable. But there has to be some sort of communication between these executable parts to make it function as an application.
Alternatively, the same executable might run on multiple JVMs and we still call it one application. In this case, there would be some sort of outside stuff that decides how to allocate the work of the application amoung the multiple JVMs. For example, you could run 18 instances of Tomcat on 9 boxes with a hardware load balancer dividing up the network requests and assigning each one to one of the Tomcat instances. In this case, though, part of the application is probably running on 1000's of user's computers inside a browser.
Sometimes, we say multiple applications are running under another application. In this case, we might call the master application a container. One example is Tomcat. In this case, Tomcat manages the load of requests for each of the separate applications because the HTTP requests come in off the network with information in the header indicating which one handles that request.
You already say that you have some code running under Tomcat. Tomcat is a single executable (It runs in one JVM and has one variety of Java) and it manages running your one Java executable supplied as a .war file, usually. There could be other Java applications running in other JVMs that communicate through that Tomcat and with your executable. Or there might not be other JVMs that your executable communicates with running somewhere else.
So, you can see, the real answer is "it depends." If you have multiple JVM communicating in some way, you could have different Java varieties. If it's all running under a single Tomcat instance, then you have a single variety of Java.

run separate background process on java web server

Basically I want to use a jsp web page, so in Java, to run(manager) different background process (could be anything that runs) on a linux server.
They need to be run as different user than the web itself.
I wonder what options do I have?
I just found out that
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("linux command");
may work.
But I don't know whether the (child?) process just started will be completely detached from the java servlet process? And is it possible to run it under different account?
Using Runtime in a supposedly lifelong running Java EE web application is a bad idea.
First and foremost, a new process created by Runtime will by design allocate as many new heap memory as the currently running Java environment. This may not necessarily harm inside a simple Java application which uses by default 64MB or something, but in a Java EE web application which ususually allocates memory in terms of gigabytes, this is going to be a complete memory waste.
Second, you just don't want so spawn unmanaged processes/threads inside a Java EE web application. What if the process/thread stalls and/or runs forever which can cause unability to shutdown/restart the Java EE web application when necessary (you'd need to kill it altogether first)? What if the process crashes and takes down your whole Java EE runtime along it?
Last, you cannot change the user running the process. It will always be the same user who has executed the currently running Java runtime.
You have basically 2 options:
Don't use Java for this at all. For example, just do the job using platform provided background job manager, such as Cron in Unix based platforms and Task Scheduler in Windows based platforms.
Do it in 100% Java. Perform the same goal using pure Java, without the need to spawn a process. You can if necessary manage background jobs using ExecutorService API or a 3rd party library like Quartz. Note that even those jobs have still to run 100% pure Java code.

Multiple java processes in Tomcat

I am working on a web-based application which is deployed in the Tomcat server. In our local dev enviroemt, when we start the Tomcat server it spawns only one java process which keeps running. However, an issue has been reported in production where the CPU usage for java process has gone up and there are multiple java processes which have been spawned.
There is no other java application running, so this must have been spawned from Tomcat itself. What is the reason that in our development enviroment there is only one java process while in production multiple java processes have been spawned by Tomcat and how to correct it.
Regards,
Dev
Unlike Apache HTTPD, Tomcat doesn't spawn processes on its own (it uses multiple threads to serve multiple clients) so you should look elsewhere. For example, how do you deploy your application to the Tomcat. Could it be something like a buggy deployment script?
Also, what other applications run on this Tomcat container?
What you see are most likely multiple threads that the version of top or ps shows on the production box, while you don't see them on the local one.
In production you most likely face a much higher workload, so that requests are served in parallel, while on the local box Tomcat gets away with less threads.

Categories

Resources