Archive d’étiquettes pour : Memory


Occasionally, installing fresh packages daily may be necessary when working in a Linux environment. To install new software, you must determine how much RAM is available. Therefore, you should be able to examine the RAM or memory installed and available on your system.

This post will examine a few key commands for CentOS 8 that help determine how much memory or RAM is available.

Prerequisites

To check the memory usage, you should have sudo privileges.

How To Check Memory Usage Details Using GUI on CentOS 8

You can easily carry out the following action if you wish to check memory usage details using the graphical user interface (GUI). In the search box for the application, enter “system preceptor”.

You can quickly check the RAM usage by selecting the “Resources” tab.

Linux Commands Used To Check the Memory Usage Details on CentOS 8

The five different methods available can help determine how much memory is in use. These methods are listed:

  1. Free command
  2. Cat command
  3. vmstat command
  4. Htop command
  5. Top command

Check Memory Usage Details Using the Free Command

The previous image displayed contains several concepts, each of which we will define individually.

  • Used memory may be calculated using the formula used memory = total – free – buffer/cache.
  • The total reflects the total memory installed on your machine.
  • Free displays the memory that is not in use.
  • Shared displays the amount of memory that is shared by various programs.
  • Buffers the memory that the OS kernel has set aside. When a process demands additional memory, this memory is allocated as buffers.
  • Cached memory is used to store recently accessed files in RAM.
  • buff/cache Memory cache + buffers
  • Available displays memory that can be used to begin new processes without swapping.

The information displayed in the previous screenshot, such as that under the words used, available, and swap memory, is in kilobytes.

You may examine the complete description and all the options of the free command by using the following command:

Check Memory Usage Details Using the “cat” Command

First, open the terminal window and type “cat /proc/meminfo”. This command displays the total memory usage and available memory information from a file “/proc/meminfo”.

This command displays the real-time details of memory usage and the information about shared memory, which is used by the buffers and kernel.

Check Memory Statistics Using the vmstat Command

To view comprehensive posible memory statistics, use the vmstat command.

The memory, system processes, CPU activity, paging, block IO, and traps are all exposed by this command.

Display Memory Usage Details Using the htop Command

Like the top command, the htop command displays information. The htop command offers a user-friendly interface and improved control options.

The htop command has an interactive interface and can scroll the page horizontally and vertically. It also uses colors to present its output and provides a complete command-line environment for all processes. To exit the current window, press “Ctrl+c”.

The following information will appear on your terminal:

  1. The information summary and visual text counts are in the top area.
  2. The comprehensive information for each procedure is shown in the middle part. It is simple to carry out the various tasks on each distinct process.
  3. You can rapidly configure and manipulate the processes without using any commands, thanks to the list of all shortcuts at the bottom of the displayed window.

The following command can be used to install the htop utility if it isn’t already on your CentOS 8 system:

Check Memory Usage Details Using the top Command

The command-line tool top helps look at how much memory and CPU each process uses. It presents details about items, such as Uptime, media load, tasks running, user logged-in information, CPU utilization, swap and memory usage, and system processes.

The top command automatically updates the information on the terminal, allowing you to track the processes’ use of RAM in real-time.

Conclusion

This article has shown us how to preceptor the memory usage details on the CentOS 8 system. Additionally, we have run other commands to display the memory information, including cat, free, vmstat, top, and htop. You may quickly find out information about your system’s RAM and CPU by using these instructions.



Source link


In DMA, the decision on memories that are allocated cannot take during the compile time. This decision or memory is allocated during the Runtime.

Whenever we create any variable through DMA, that type of variables do not have any name; we access these variables through address or pointer.

In SMA, Programmer knows from earlier time that how many Variables or How many memories are required for his/her program.

But In DMA,  programmer does not know from earlier state that how many variables or memory is required, it depends upon the user’s requirement.

Types of DMA:

  1. malloc ()
  2. calloc ()
  3. realloc ()
  4. Free ()

malloc ()

malloc () function is an action statement when compiler read this line. Compiler doesn’t understand how many memories is allocated as it is an action statement. In runtime memory block is created.

Whenever we call malloc () we pass a number as an argument, which it can understand the number of bytes of memory block are to be created by the malloc (). In malloc (), it can’t declare any data type. Malloc () always return the address, which memory block is created.

Malloc () return type is a void pointer because it doesn’t know which types of address it returns. For this we have to type caste.

Here we type caste, because malloc () is a void pointer.

Example-1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include<stdio.h>

#include<stdlib.h>

#define NULL 0

int main ()
{
    int *a , *t ;
    int size ;
    printf ( » what is the size of the table ? « ) ;
    scanf(«%d»,&size);
    printf ( » n « ) ;
    if ( ( t = ( int* ) malloc ( size * sizeof ( int ) ) )  == NULL )
    {
    printf( » No space available n « ) ;
    exit ( 1 ) ;
    }
    printf ( » n Address of the first byte is %un « , t ) ;
    /* Reading table values*/
    printf ( » n Input table values n « ) ;
    for ( a = t ; a < t + size ; a++ )
    scanf(«%d», a);
    /* Printing table values in reverse order*/
    for ( a = t + size 1 ; a >= t ; a )
    printf ( » %d is stored at address %u n «, *a , a ) ;
    free ( t ) ;
    return 0 ;

Output:

Calloc ():

With the help of calloc () we can create more than one block or array in calloc (we pass two arguments; 1st one is how many blocks we want to create & 2nd one is the size of the block). calloc () also return address in each block by default 0 is exist.

Example-2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include<stdio.h>

#include<stdlib.h>

int main ()
{
    int *n , *freq , i , size ;
    printf ( » what is the size of the list ? « ) ;
    scanf(«%d»,&size);
    n = ( int* ) malloc ( size * sizeof( int ) ) ;
    printf ( » Enter the numbers: « ) ;
    for ( i = 0 ; i < size ; i++ )
    {
        printf ( » n enter the num[%d]: «,i ) ;
        scanf(«%d»,&n[i]);
        if ( n [ i ] < 0 || n [ i ] > 4 )
            {
                printf ( » n Number should be within range (0-4) « ) ;
                i ;
                continue ;
            }
    }
    freq = ( int * ) calloc ( 5 , sizeof ( int ) ) ;
    for ( i = 0 ; i < size ; i++ )
            freq [ n [ i ] ]++ ;
    printf ( » n The frequencies of the numbers are: « ) ;
    for ( i = 0 ; i < 5 ; i++ )
            printf ( » n freq [%d] = %d « , i , freq [ i ] ) ;
    printf ( » n « ) ;
    free ( freq ) ;
    return 0 ;
}

Output:

realloc ()

Whenever we create a block with the help of malloc () or calloc () & we want to change or resize the block, we use realloc ().

1

Void *realloc (void *block, int size)

In realloc() we have to pass the address as an argument from which block we want to resize.

and the size of the block we want to resize. That size we have to pass an argument in realloc ().

Only those blocks which are created by malloc () or calloc () can be resized by realloc ().

Example-3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define NULL 0

int main()

{

char *buffer ;

/* Allocating memory */

if ( ( buffer = ( char * ) malloc ( 10 ) ) == NULL )

{

printf (» malloc failed. n « ) ;

exit ( 1 ) ;

}

printf ( » Buffer of size %d created n « , sizeof (buffer) );

strcpy ( buffer , » HYDERABAD « ) ;

printf( » n Buffer contains: %s n « , buffer ) ;

/* Reallocation */

if ( ( buffer = ( char * ) realloc ( buffer , 15 ) ) == NULL )

{

printf ( » Reallocation failed. n « ) ;

exit ( 1 ) ;

}

printf ( » n Buffer size modified. n « ) ;

printf ( » n Buffer still contains: %s n « , buffer ) ;

strcpy ( buffer , » SECUNDERABAD « ) ;

printf ( » n Buffer now contains: %s n « , buffer ) ;

/* Freeing memory */

free ( buffer ) ;

return 0 ;

}

Output:

free ()

With the help of free (), we release the memory block that is created by malloc () or calloc () or realloc ().

Static variables only exist into the scope of the block or a function. If we cannot run the free (), whenever static variable p is destroyed, the variable which is created dynamically, that are not destroyed, but stayed forever in RAM or in memory. This is called memory leak. For this free () is required to destroy the memory block that is created dynamically.

Free () only destroy those memory that is created dynamically.

Conclusion:

DMA is a powerful concept in C language because it removes the drawback of SMA. In SMA we have to make decision before running the program that how many memory blocks are created. As a result, memory is wasted or memory is not enough. DMA resolve the problem by taking decision on run time that how many block are required to allocate memory. It allocates memory to the requirement of the program.



Source link