Good day!

There is a question of the following nature: I have a cache in the program that is cleared if maxMemory is totalMemory (values ​​are taken from runtime) less than 10mb. Clearing the cache is the usual re-creation of some structure and one of the members of which is Image. When re-creating image = null; nevertheless, at a certain stage, my memory runs out (java heap overflow).

Actually the question is: what am I doing wrong, why does memory still end with time?

  • if the memory is running out, then usually the problem is: 1) your program itself requires a lot of resources and you run the JVM with standard settings (google 'java memory options') 2) you have something in the "C" memory leak - you create many objects to store which all the memory is wasted (usually a problem occurs if the object is created in a cycle + a logical error in the program operation). the garbage collector cleans the memory itself, but objects are deleted only if they are not referenced (this may mean that you store too much unnecessary data) - jmu
  • ^^ continued: if you have the correct code, then you need to solve the problem with item 1 + see the answer @ArTeam (more optimal use of memory when the cache is running ) - jmu
  • It is possible that your manual memory cleaning simply does not have time to create new objects and then a boom happens. You need to do this job on the JVM, as suggested by @ArTeam. - cy6erGn0m
  • 10mb is very small. But I think this is not the case, but the fact is that in fact you do not release the memory (there are references from somewhere). Try cleaning the cache all the time (for the test) and if heap overflow, then look for where they hang. - avp
  • Whatever one may say, but still there is not enough manual object liquidator in java. - skegg

1 answer 1

Read about cache refresh strategies and weak and flexible links (for example, here and here ).

You can read about caching strategies here .