我只是想跟踪各个进程正在使用多少内存(不同的选项可以从根本上改变这个特定应用程序的内存使用情况)。我不喜欢其他地方描述的各种“查找进程 pid 和 grok /proc/pid/smaps”解决方案......
是否有替代命令可以仅转储特定进程的内存使用情况?记忆?
以下代码在 ubuntu 上构建时会创建一个可执行文件。
#include <stdio.h>
void otherfunc(FILE* fout){
fclose(fout);//Line 4
fout = fopen("test.txt", "w");//Delete contents and create a new file//Line 5
setbuf(fout, 0);//Line 6
}
int main() {
FILE *fout = fopen("test.txt", "r");//Line 10
if (fout) {
//file exists and can be opened
fclose(fout);//Line 13
fout = fopen("test.txt", "a");//Line 14
setbuf(fout, 0);
}
else {
//file doesn't exists or cannot be opened
fout = fopen("test.txt", "a");//Line 19
}
otherfunc(fout);//Line 22
fclose(fout);//Line 24
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当运行 valgrind 时,valgrind 给出以下警告:
==13569== …
我正在使用 valgrind 使用此命令查找我的程序的内存使用情况
valgrind --tool=memcheck --leak-check=full -s --track-origins=yes ./memoryProblem
Run Code Online (Sandbox Code Playgroud)
它显示我的程序的总堆使用量为 72,704 字节
这是我的程序
#include <iostream>
int main(int argc, char const *argv[])
{
int a[32768];
std::cout << sizeof a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
int 是 4 个字节,32768*4 应该是 131,072 个字节,这也是程序的输出,但是为什么 valgrind 显示堆栈上数组的堆使用情况?
此外,我删除了 iostream 和 cout 并将数组大小减少到 10 个整数,这是输出:
==169343== Memcheck, a memory error detector
==169343== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==169343== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==169343== Command: ./memoryProblem
==169343== …Run Code Online (Sandbox Code Playgroud) 我有一个包含 3 条染色体字符串的文件,我想将其连接成一个基因组。然后我必须跨多个线程访问这个串联字符串(我使用 pthread_t)。为此,我必须在提取数据时使用 pthread_mutex_lock,然后使用 strcat 连接使用 const char* 函数 fai_fetch 提取的数据,然后将数据保存为 char* (见下文)。
// genome_size the size of all the chromosomes together
// chr_total the number of chromosomes I wish to concatenate
char* genome = (char*) malloc(sizeof(char) * (genome_size+chr_total));
for (int i = 0; i < chr_total; i++){
pthread_mutex_lock(&data_mutex);
const char *data = fai_fetch(seq_ref,chr_names[i],&chr_sizes[i]);
pthread_mutex_unlock(&data_mutex);
//sprintf(&genome[strlen(genome)],data);
strcat(genome,data);
//sprintf(genome+strlen(genome),data); //All three gives conditional jump or move error
//sprintf(genome,data); // THIS SOLVES VALGRIND ISSUE ONE BUT DOES NOT GIVE A CONCATENATED …Run Code Online (Sandbox Code Playgroud) 我正在编写一个相当长的程序,其中包含大量数据导入,但我开始收到错误munmap_chunk(): invalid pointer。我环顾四周,这似乎是free()功能造成的。然后我在程序中注释了所有此类函数,但错误仍然发生。我所能发现的是,这可能是由内存问题引起的,我应该运行 Valgrind。所以我这样做了,它返回了一大堆错误,主要与我的导入函数有关。特别是这个:
void import_bn(int depth, int idx, float pdata[4][depth]) {
// [0][:] is gamma, [1][:] is beta, [2][:] is moving mean, [3][:] is moving variance
// Define name from index
char name[12]; // maximum number of characters is "paramxx.csv" = 11
sprintf(name, "param%d.csv", idx);
// open file
FILE *fptr;
fptr = fopen(name, "r");
if (fptr == NULL) {
perror("fopen()");
exit(EXIT_FAILURE);
}
char c = fgetc(fptr); // generic char
char s[13]; // string, maximum number …Run Code Online (Sandbox Code Playgroud) Valgrind 怎么说:
==13233== Thread 4:
==13233== Invalid write of size 4
==13233== at 0x13AC1F: Server::listener_thread() (realsense_multicam.cpp:136)
==13233== by 0x4E456DA: start_thread (pthread_create.c:463)
==13233== by 0x98F471E: clone (clone.S:95)
==13233== Address 0x1377d938 is 1,192 bytes inside a block of size 1,256 free'd
==13233== at 0x4C3323B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13233== by 0x14A96F: __gnu_cxx::new_allocator<Server>::deallocate(Server*, unsigned long) (new_allocator.h:125)
==13233== by 0x149489: std::allocator_traits<std::allocator<Server> >::deallocate(std::allocator<Server>&, Server*, unsigned long) (alloc_traits.h:462)
==13233== by 0x1476AB: std::_Vector_base<Server, std::allocator<Server> >::_M_deallocate(Server*, unsigned long) (stl_vector.h:180)
==13233== by 0x145B3B: void std::vector<Server, std::allocator<Server> >::_M_realloc_insert<Server&>(__gnu_cxx::__normal_iterator<Server*, std::vector<Server, std::allocator<Server> …Run Code Online (Sandbox Code Playgroud) 我正在尝试用 C 创建一个向量,以下是结构声明:
#define VECT_INITIAL_CAPACITY 4
typedef struct vect vect_t;
struct vect {
char **data; // data is an array of strings
unsigned int size;
unsigned int capacity;
};
Run Code Online (Sandbox Code Playgroud)
我有一个函数可以构造一个新的空向量,这就是我所做的:
vect_t *vect_new() {
vect_t *v = (vect_t*) malloc(sizeof(vect_t));
if (v == NULL) {
return NULL;
}
// Allocate memory for data
v->data = (char**) malloc(VECT_INITIAL_CAPACITY * sizeof(char*));
if (v->data == NULL) {
return NULL;
}
for (int i = 0; i < VECT_INITIAL_CAPACITY; i++) {
v->data[i] = NULL;
}
// …Run Code Online (Sandbox Code Playgroud) typedef struct Model
{
int recordId;
char *name;
}Model;
typedef struct ModelArray
{
//keeps the size that the array was initially create with. When more elements are needed
//we use this to add that many more elements
int originalSize;
//total number of elements that can be used
int elements;
//total number of elements used
int count;
//the actual array is stored here
Model *source;
}ModelArray;
void initModelArray(ModelArray *array, int numberOfElements)
{
array->originalSize = numberOfElements;
array->elements = numberOfElements;
array->count = 0; …Run Code Online (Sandbox Code Playgroud) 我有一个简单的程序,只用数字填充数组并打印每个数字.
但是,valgrind声称至少有8000字节的内存泄露.
int main(int argc, char *argv[])
{
int numbers[10];
int i = 0;
for(i = 0; i < 10; i++)
{
numbers[i] = i;
printf("%d",numbers[i]);
}
free(numbers);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释内存泄漏的位置吗?
这是为阵列释放空间的正确方法吗?
我已经经历了几个有关相同错误代码的类似问题,最关心的是传递无效指针地址.但是,这看起来并非如此,因为我的程序正确地处理了传递的任何地址.
FORMAT_STRING:
static char *format_string(char *string)
{
int i;
if (string == NULL) {
return string;
}
string[0] = (char)toupper((int)string[0]);
111->for (i = 1; string[i] != '\0'; i++) {
if (!isalpha(string[i-1])) {
string[i] = (char)toupper((int)string[i]);
} else {
string[i] = (char)tolower((int)string[i]);
}
if (string[i] == '/' || string[i] == '\\') {
string[i] = ' ';
}
}
/* Remove End of string White Spaces */
while (string[--i]==' '); string[++i]='\0';
return string;
}
Run Code Online (Sandbox Code Playgroud)
metadata_extract:
static int metadata_extract(const char *filename, struct kw_metadata …Run Code Online (Sandbox Code Playgroud)