'Why doesn't GC collect objects without references?
public static void main(String[] args) {
try {
Path file = Paths.get("file.jar"); // 100mb file
for (int i = 0; i < 10; i++) {
byte[] toCollect = Files.readAllBytes(file);
toCollect = null;
}
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
new Thread(() - > {
try {
Thread.sleep(100000000L);
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
After executing this code the program uses 1GB of RAM, why?
This code reads bytes from a file using the JAVA API and puts them into an array, then assigns null to that array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
