Java - programmatically reduces the load on the application when memory runs out

No, really, this is what I'm trying to do. The server supports 1600 users - a long, long process, not a web server, but sometimes users generate more activity than usual, so you need to reduce the load, especially when the "resources", which to a large extent means heap memory, run out. This is a big design question - how to do it?

Perhaps this will be due to the prevention of OOM instead of recovery. Perfectly

if(nearlyOutOfMemory()) throw new MyRecoverableOOMException(); 

may happen.

But this function nearlyOutOfMemory() I really don't know what could be.

+4
source share
4 answers

Divide the server into fragments, each of which contains fewer users, but lives on different physical machines.

If you have a lot of caches, try using soft links that will be cleared when the VM runs out of heap.

In any case, the profile, profile, profile is first to see where CPU time is consumed and the memory is allocated and held.

+3
source

I really asked a similar question about OOM processing, and it turned out that there are not many options for recovery. Basically you can:

1) call the outer shell script ( -XX:OnOutOfMemoryError="cmd args;cmd args" ), which will trigger some action. The problem is that if OOM occurred in some thread that does not have a decent recovery strategy, you are doomed.

2) Define the threshold for the old generation, which is technically not OOM, but a few steps forward, say, 80% and is valid if the threshold is reached. More details here .

+1
source

You can use Runtime.getRuntime() and the following methods:

But I agree with other posters, using SoftReference , WeakReference or WeakHashMap will probably save you from having to manually recover from this condition.

+1
source

Can also be used throttling, regulating the servlet filter. I ran into a DoSFilter jetty / eclipse.

0
source

All Articles