2

We have an application that keeps hanging, and it runs on Java (usually running on a Solaris server). Every time it hangs we have to kill the process. But we can't tell whether Java's garbage collector actually does anything once the process is killed. We're concerned about having to reboot the server to free the memory all the time this application hangs just to free the memory.

Juancho
  • 21
  • 1
  • If you're killing the JVM, there is no garbage collector anymore. – RealSkeptic Aug 11 '16 at 12:55
  • 1
    All process allocated memory is freed when you kill a process. – Uwe Allner Aug 11 '16 at 12:57
  • I guess what worries me here is that I've been told that because JVM is written in C, this software needs to de-allocate memory before exiting, otherwise the memory is not freed. And what keeps track of the memory allocated are the Application or JVM themselves, not the OS, the OS simply helps in the transaction of allocating or de-allocating the memory. Which is why a restart was the only way to free memory when I didn't allocate memory correctly programming in C. Am I correct? – Juancho Aug 12 '16 at 13:53
  • 2
    @Juancho *I've been told that because JVM is written in C, this software needs to de-allocate memory before exiting, otherwise the memory is not freed.* No. That is so wrong. The only memory that won't be released would be shared memory, but only if the process creates [shared memory segment(s)](https://en.wikipedia.org/wiki/Shared_memory#Support_on_Unix-like_systems). Note that a strict Java process isn't going to be able to do that. Do you have root access? Post the output from `echo ::memstat | mdb -k`. That will show what's using actual physical memory. Also, `pstack PID` is useful. – Andrew Henle Aug 12 '16 at 15:56
  • 1
    This appears to be an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You have a process that hangs - and you're assuming it's memory related, so you're asking about memory usage. The real problem is the process is hanging. – Andrew Henle Aug 12 '16 at 16:00
  • @AndrewHenle I'm actually starting to get interested in what's causing the process to hang! – SusanW Aug 15 '16 at 11:32
  • @juancho do you want to raise the "hanging process" issue as a separate question? – SusanW Aug 15 '16 at 11:33
  • 1
    @SusanW In case the OP is watching: Run `pstack PID` several times, replacing `PID` with the process ID of the stuck process. The toughest part of doing this with a stuck Java process is figuring out which of the many threads is the one (or more) that are stuck, as a Java process will have many threads started by the JVM itself that have nothing to do with your code. You need to find the thread(s) running your code and see what they're actually doing - or what call(s) they are stuck in. – Andrew Henle Aug 16 '16 at 10:21
  • @AndrewHenle .... for which a JMX tool, like `jvisualvm` or `jconsole` can be indispensible. Wouldn't it be wonderful if there was some kind of "Documentation" project somewhere around here we could put all these gems into and then reference? :-) – SusanW Aug 16 '16 at 11:50

2 Answers2

3

The Java Garbage Collector runs as part of the Java process (actually as one or more threads), to manage the usage of the space assigned by the operating system. Once you kill the process, all of that stops, and the memory assigned to that process is freed, just as it would be for any other process.

You don't have to reboot or take any other action.

SusanW
  • 1,550
  • 1
  • 12
  • 22
2

If you kill the process, Java's GC doesn't need to bother anymore. Any memory allocated by the process is released regardless of the GC.

Kayaman
  • 72,141
  • 5
  • 83
  • 121