I have encountered a bug where one of our server applications was using more and more memory every second pretty much and I have managed to filter out a short example that still shows that behavior:
public class TestGetLastModifiedTime {
private static final Path PATH = Paths.get("D:\\test.txt");
private static final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(1);
public static void main(String[] args) {
SCHEDULER.scheduleAtFixedRate(() -> getLastModifiedTime(), 0, 1, TimeUnit.SECONDS);
}
private static void getLastModifiedTime() {
try {
FileTime lastModifiedTime = Files.getLastModifiedTime(PATH);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
}
Running on Windows 8.1 and Java 8u20.
Via VisualVM I have observed that the maximum heap size does not grow and that the heap itself keeps increasing. Simultaneously I observe in Windows Task Manager that the spawned java.exe process keeps using (reserving) more memory every second.
The interesting part is that when I Perform GC from within VisualVM, all the used heap memory gets reset to practically zero and the used memory of the java.exe process does not shrink, as expected, as it is considered reserved.
However, after the GC has been done, then the memory usage still increases every second, while now there is surely enough free heap space.
The Metaspace is also unaffected.
To me this really smells and looks like the JVM has a memory leak.
Can anyone help me out and explain what is going on here?
File
object, usingFile.lastModified()
. – Nigeria-Xmx
to 8 MB or so. See if it goes out of memory. – NigeriaFiles.getLastModifiedTime()
, but that's easy to check: run in an endless loop, and if the program isn't killed you know it's not to blame. More likely is the scheduled executor pool, but I suspect that what you're seeing is a misleading number that includes the stack allocationss of dead threads. – Nickynico