Home > Software engineering >  cleaning the computer's RAM or searching for unnecessary elements in it java
cleaning the computer's RAM or searching for unnecessary elements in it java

Time:06-07

Can I clear the PC's RAM by calling some method or something like that. If this is not possible, then how could I look for unnecessary elements in memory and clear them

CodePudding user response:

You can't touch any memory outside the JVM at all, and all you can do inside the JVM is suggest that it clean up unused memory, through System.gc(). What actually happens when you call System.gc() is a "best effort", but according to the documentation (my emphasis)

There is no guarantee that this effort will recycle any particular number of unused objects, reclaim any particular amount of space, or complete at any particular time, if at all, before the method returns or ever.

CodePudding user response:

You can signal an intent to clear program's memory by System.gc(). It is not guaranteed to happen the moment you call it, though, but "some time in the future".

I do not think you can access your PC's memory through Java, and if you somehow can, I think that is not supposed to happen ;)

If you want to see what is in the memory available to a Java program, you need to use a profiler (like e.g. JProfiler).

  • Related