View the available dynamic memory of the current system (C)

Source: Internet
Author: User

Recently, a system with a low memory size is only kb. When it is very busy, I wrote a function to view the available memory. The maximum memory size is kb, print out all available memory blocks,
The minimum unit is 512B.
[Cpp]
1. # define MAX_MEM 512*1024 // maximum available memory
2. # define MAX_MEM_PEICES_NUM 200 // The maximum number of memory fragments that can be recorded
3. # define MIN_MEM 512 // minimum size of memory fragments to be searched
4. void ct_CalcFreeMem (void)
5 .{
6. int curFreeSize = MAX_MEM;
7. int * pt = NULL;
8. // Calc freee mem
9. int * ptArray [MAX_MEM_PEICES_NUM];
10. int I = 0, j = 0;
11. int totalMem = 0;
12. printf ("===============================\ n ");
13. while (1 ){
14. pt = (int *) malloc (curFreeSize );
15. if (pt! = NULL ){
16. printf ("Free mem slice % d = % d B \ n", I, curFreeSize );
17. ptArray [I] = pt;
18. pt = NULL;
19. I ++;
20. totalMem + = curFreeSize;
21 .}
22. curFreeSize-= MIN_MEM;
23. if (curFreeSize <MIN_MEM ){
24. break;
25 .}
26 .}
27.
28.
29. for (j = 0; j <I; j ++ ){
30. if (ptArray [j]) {
31. free (ptArray [j]);
32. ptArray [j] = NULL;
33 .}
34 .}
35. printf ("Total free mem = % d B \ n", totalMem );
36. printf ("===============================\ n ");
37 .}

 

From herbert's knowledge base

Related Article

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.