How to solve the common memory usage problems in Ruby
This article mainly explains "how to solve the common memory usage problems in Ruby". The explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the common memory usage problems in Ruby".
Memory usage is the most common problem in Ruby languages, which is also common in other JVM-based languages (yes, Java, talking about you). In fact, Ruby's garbage collection (GC) mechanism is very similar to Java. If used improperly, it can cause a lot of trouble (as shown in the following figure). In such cases, GC stops operation, consumes the overall CPU resources of the server, and even causes the server to restart.
Unicorn and Ruby memory leaks caused server downtime.
How to solve?
Restart your Ruby regularly
If you are using a popular Unicorn Web server, the memory problem may be more serious-- this is because Unicorn uses the fork mechanism. During the fork process, it copies all the contents of the parent memory (CoW for short). Therefore, you may need to use the "Unicorn Worker Killer" gem to monitor the server and restart when memory reaches a new high or the number of requests increases to a certain level. Because the gem supports randomization, the server itself is likely to be unaffected.
2. Install this killer:
Third, adjust memory
If you are using Ruby 2.x, you can better use the CoW mechanism by configuring Unicorn.
Config/unicorn.rb
1) worker_processes: 1x computing core
2) timeout: the job request timed out and should be set in the range of 15 to 30 seconds.
3) preload_app: enable CoW, but require management of connections / disconnections on fork
IV) seriously consider GC configuration
Dynamic language has some shortcomings, but the correct design can help it to run online and smoothly all the time.
Thank you for your reading, the above is the content of "how to solve the common memory usage problems in Ruby". After the study of this article, I believe you have a deeper understanding of how to solve the common memory usage problems in Ruby, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!