The paradox of garbage collection
The so-called "Chengye defeated Xiao". Java garbage collection does bring a lot of benefits and facilitates development. However, in some high-performance, high concurrency, garbage collection has become a bottleneck in Java application constraints. At present, the JDK garbage collection algorithm, always can not solve the garbage collection when the pause problem, because this pause seriously affected the program's corresponding time, causing congestion or accumulation. This is also an important reason for subsequent JDK additions to the G1 algorithm.
Of course, the above is from a technical point of view to solve the problem of garbage collection, but from the system design aspects we need to ask:
Do we need to allocate such large memory space to the application?
Can we design our system by effectively using memory rather than by expanding memory?
What we have in our memory.
What do I need to put in memory? Personally, the need to put in memory is what your application needs to be used again in the near future. Think about it, if you don't use these things in the future, why put it in memory? Is it better to put files and databases? These things generally include:
1. System Run-time Business-related data. For example, session of Web application, session of instant message and so on. These data typically need to exist during a user access cycle or in a usage process.
2. Caching. The cache is much more, you want to quickly access can be put in here. In fact, the above business data can also be interpreted as a cache.
3. Threads.
Therefore, we can not think that if we do not put business data and caching in the JVM, or to separate them out, then the Java application will need to use the memory would be greatly reduced, and garbage collection time will be reduced accordingly.
I think it's possible.
The way to solve it
Database, File system
Putting all the data into a database or file system is the easiest way to do it. In this way, the Java application memory is essentially equal to the memory required to process a peak concurrent request. The acquisition of data is obtained from the database and file system on each request. It can also be understood that after a business visit, all objects can be recycled.
This is one of the most efficient ways to use memory, but it is inefficient from an application perspective.