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