0Pricing
CUDA Academy · Aula

Encontrando vazamentos com memcheck

Acessos fora dos limites e ponteiros inválidos do dispositivo.

Encontrando vazamentos com memcheck é uma aula grátis de CUDA Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de CUDA Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de CUDA Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

Why memcheck Exists

GPU memory bugs rarely crash loudly, so compute-sanitizer with the memcheck tool watches every access and reports the trouble for you. 🛡️

The Modern Command

Run your program under the memcheck tool by prefixing it with compute-sanitizer, the modern replacement for the old cuda-memcheck.

compute-sanitizer --tool memcheck ./vecadd

Out-of-Bounds Reads

The classic bug memcheck catches is an out-of-bounds access, where a thread reads or writes past the end of a device array.

A Missing Bounds Check

Forgetting the guard lets extra threads run wild, so this index can stray beyond n and trigger an invalid global write.

int i = blockIdx.x * blockDim.x + threadIdx.x;
out[i] = a[i] + b[i];

Reading the Report

memcheck prints the error kind, the offending address, and the thread that caused it, pointing you straight at the bad access.

Get Line Numbers

Compile with -lineinfo so memcheck can map each error back to the exact source line instead of a raw address.

nvcc -lineinfo vecadd.cu -o vecadd

Detecting Leaks

Turn on the --leak-check option and memcheck reports any device memory you allocated but never released before exit.

compute-sanitizer --leak-check full ./vecadd

A Classic Leak

Allocating without a matching free leaks the buffer, and across many iterations that slowly exhausts device memory.

cudaMalloc(&d_a, bytes);
// ...used, but cudaFree(d_a) is missing

Invalid Device Pointers

memcheck also flags passing a host pointer where the kernel expects a device pointer, a mistake that silently corrupts results.

Misaligned Access

Some loads require aligned addresses, so memcheck warns about a misaligned access that would otherwise produce garbage or a fault.

Fix and Rerun

After each fix, run memcheck again until it reports zero errors. A clean run is your sign the memory bugs are gone.

Quick Check

What kind of bug is the memcheck tool primarily designed to find?

Recap

You ran memcheck, added -lineinfo for source lines, enabled leak checking, and learned its common error kinds. Clean runs mean safe memory. ✅

Perguntas Frequentes

A aula “Encontrando vazamentos com memcheck” é grátis?

Sim — o texto completo de “Encontrando vazamentos com memcheck” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de CUDA Academy, atualize para CoddyKit PRO. O curso de CUDA Academy inclui 4 aulas no total.

O que vou aprender em “Encontrando vazamentos com memcheck”?

Acessos fora dos limites e ponteiros inválidos do dispositivo. Você pratica CUDA Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar CUDA Academy?

Nenhuma experiência prévia é necessária. CUDA Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Encontrando vazamentos com memcheck”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de CUDA Academy?

Sim. Cada aula de CUDA Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Percorrendo kernels no cuda-gdb
  2. Encontrando vazamentos com memcheck
  3. Caçando condições de corrida com racecheck
  4. Detectando erros de sincronização com synccheck
← Voltar para CUDA Academy