Linux查看内存代码
时间: 2024-02-12 11:59:21 浏览: 43
Linux查看内存的命令有很多,以下是常用的几个命令:
1. free:显示系统内存使用情况,包括总内存、已用内存、空闲内存等。
2. top:实时显示进程信息,包括CPU、内存使用等信息。
3. ps:显示当前进程信息,包括进程ID、内存使用等信息。
4. vmstat:显示虚拟内存统计信息,包括内存使用、磁盘IO等信息。
5. sar:系统性能分析工具,可以查看CPU、内存、磁盘IO等信息。
如果需要查看更详细的内存信息,可以使用以下命令:
1. cat /proc/meminfo:显示系统内存信息,包括总内存、空闲内存、缓存等信息。
2. cat /proc/buddyinfo:显示内存碎片信息。
3. cat /proc/slabinfo:显示内核缓存信息。
需要注意的是,以上命令需要在Linux终端中输入,并且需要具有足够的权限才能执行。
相关问题
linux内存池代码
这里是一个简单的 Linux 内存池代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define POOL_SIZE 1024
typedef struct {
int size;
char data[POOL_SIZE];
} mem_block;
mem_block* pool = NULL;
int pool_index = 0;
void* my_malloc(int size) {
if (pool == NULL) {
pool = (mem_block*)malloc(sizeof(mem_block));
pool->size = POOL_SIZE;
}
if (size > POOL_SIZE) {
return malloc(size);
}
if (pool_index + size > POOL_SIZE) {
return NULL;
}
void* ptr = &pool->data[pool_index];
pool_index += size;
return ptr;
}
void my_free(void* ptr) {
// do nothing
}
int main() {
int* p1 = (int*)my_malloc(sizeof(int));
*p1 = 42;
char* p2 = (char*)my_malloc(10);
sprintf(p2, "hello");
printf("%d %s\n", *p1, p2);
return 0;
}
```
这个内存池的实现非常简单,它使用一个静态的内存块作为内存池,当需要分配内存时,它会从内存池中分配一段连续的空间,并返回指向这段空间的指针。如果内存池中的空间不足,它会返回 NULL。当需要释放内存时,它什么也不做,因为这个内存池是静态的,无法释放。
linux共享内存通信代码
下面是一个简单的 Linux 共享内存通信的代码示例:
```c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SHM_SIZE 1024
int main()
{
int shmid;
char *shmaddr;
struct shmid_ds buf;
int flag = 0;
key_t key = ftok(".", 'a');
if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) == -1)
{
perror("shmget error");
exit(1);
}
if ((shmaddr = shmat(shmid, 0, 0)) == (void *)-1)
{
perror("shmat error");
exit(1);
}
while (1)
{
printf("Please input your message: ");
fgets(shmaddr, SHM_SIZE, stdin);
if (strncmp(shmaddr, "quit", 4) == 0)
break;
if (shmctl(shmid, IPC_STAT, &buf) == -1)
{
perror("shmctl error");
exit(1);
}
printf("shm_segsz = %d bytes\n", buf.shm_segsz);
printf("shm_cpid = %d\n", buf.shm_cpid);
while (strncmp(shmaddr, "quit", 4) != 0)
{
if ((flag = strcmp(shmaddr, "")) != 0)
{
printf("You wrote: %s", shmaddr);
memset(shmaddr, 0, SHM_SIZE);
}
}
}
if (shmdt(shmaddr) == -1)
{
perror("shmdt error");
exit(1);
}
if (shmctl(shmid, IPC_RMID, 0) == -1)
{
perror("shmctl error");
exit(1);
}
return 0;
}
```
这个程序创建了一个大小为 1024 字节的共享内存,然后进入一个循环,等待用户输入消息。用户输入的消息会被写入共享内存中,然后程序会从共享内存中读取消息并输出。如果用户输入 "quit",程序会退出循环并删除共享内存。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)