A complete garbage collection mechanism for fixed unit size

Source: Internet
Author: User

Sometimes in order to save space, we need garbage collection. The efficiency of the program can be affected by the inefficient disposal of malloc and free garbage collection units. In fact, there is a simple way to achieve, is based on the cyclic queue of garbage collection device.

Why malloc is inefficient

The general system malloc functions are actually very advanced and can handle most applications. However, its efficiency is still relatively low. malloc needs to handle units of different sizes, which need to be stored in contiguous memory space to speed up access and improve the hit rate. Even with and without a set maintenance, the time complexity in extreme conditions is O (nα (n)). Obviously, it is very effective to apply for large chunks of memory at once.

malloc is a large constant because of its complex implementation.

How to do garbage collection

Garbage collection is regarded as a very figurative noun. Some of the open memory, no need to return the free space. It means that the things you have malloc, need free to go back, or new things to self-destruction, in the destructor to explicitly specify free, or the runtime environment provides a stealth GC, when there is no pointer to an object when the object is GC off, these are an effective way to improve memory utilization. However, only malloc and free cannot effectively manage memory and can easily cause memory fragmentation.

Do your own resource allocation, garbage collection

I like to pre-apply large memory to the node a large number of dynamically created, recycled objects to alleviate the burden of the m/f function. It is undeniable that not managing dynamic allocations effectively still causes memory fragmentation. However, there is a complete garbage collection, allocation scheme for the node class object that is fixed for this type of cell size (that is, the memory size allocated each time). That is, regardless of the sequence of operations, as long as the memory size is greater than or equal to the total node size at any one time, the request can be satisfied.

1 General GC

Write a normal GC that can be maintained with stacks. However, this space utilization can be very low.

2 Queue GC

Free memory can be opened at both ends of the queue.

Similarly writable cyclic queue GC

3 Implementation of full GC

The implementation of a full GC is very simple, and the core idea is to maintain pointers with circular queues so that the allocated memory is contiguous on the loop queue, not necessarily on the memory itself. Based on the RAM model, this approach works well.

struct Sn{data dat;int puid;}; typedef sn* SNP;STRUCT s_buf{#define CCQ (x) x= (++x==tbuf)? 0:x;sn Buf[tbuf];int f,e,id[tbuf];inline SNP alloc () {CCQ (E) ( Buf+id[e])->puid=id[e];return buf+id[e];} inline void Free (SNP node) {CCQ (f) id[f]=node->puid;} inline void init () {for (int i=0;i<tbuf;++i) id[i]=i;}} Sbuf;

The proof is simple, leave it as a problem.

A complete garbage collection mechanism for fixed unit size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.