Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityGetting Repeated No Caller ID Calls? Here’s What’s Really Going On(22.09.2026 um 22:31 Uhr)
Windows Tipps & SecurityHöllenmaschine: Gaming-Peripherie für gut 1.800 Euro für die HMX 6(23.09.2026 um 10:20 Uhr)
Windows Tipps & SecurityDas nächste große Ding: KI-Agenten(23.09.2026 um 10:30 Uhr)
Sichere ProgrammierungHow AI Is Making Restaurant Menus Easier to Navigate(23.09.2026 um 10:55 Uhr)
Windows Tipps & SecurityGetting Repeated No Caller ID Calls? Here’s What’s Really Going On(22.09.2026 um 22:31 Uhr)
Windows Tipps & SecurityHöllenmaschine: Gaming-Peripherie für gut 1.800 Euro für die HMX 6(23.09.2026 um 10:20 Uhr)
Windows Tipps & SecurityDas nächste große Ding: KI-Agenten(23.09.2026 um 10:30 Uhr)
Sichere ProgrammierungHow AI Is Making Restaurant Menus Easier to Navigate(23.09.2026 um 10:55 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Tech Interview Series: What Happens When You malloc 2 GB but Don't Use It?

What Happens When You malloc 2 GB but Don’t Use It? malloc reserves virtual memory, not physical memory: When you call malloc(2GB), the operating system reserves 2 GB of address space for your process in the virtual memory. No p…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




What Happens When You malloc 2 GB but Don’t Use It?





  1. malloc reserves virtual memory, not physical memory:




    • When you call malloc(2GB), the operating system reserves 2 GB of address space for your process in the virtual memory.

    • No physical RAM is allocated yet because you haven’t accessed (touched) the memory.




  2. RES and VIRT behavior:





    • VIRT will increase by 2 GB because the reserved memory adds to your process's virtual address space.


    • RES will not increase at this point because physical memory (RAM) has not been allocated.




  3. When does RES increase?




    • Physical memory is only allocated when you touch (read/write) the memory.


      • For example, if you write to a page of the allocated memory, the kernel will allocate a physical RAM page to your process. This will reflect in the RES size.













Question:



If I malloc 2 GB of memory but don’t use it (i.e., the pages are allocated virtually but not yet touched), will it count toward the RES (resident memory)? And since the memory hasn't been touched, can other processes still use that space?




1. If I malloc 2 GB and do not use it, will it be counted in RES?



No, it will not be counted in RES.



Here’s why:




  • When you call malloc(2GB), the operating system reserves virtual address space for the requested memory.

  • However, no physical memory (RAM) is allocated until you actually access or "touch" those pages.

  • This is due to the lazy allocation strategy used by modern operating systems. Pages are only backed by physical memory (loaded into RAM) when they are accessed for the first time.



In top command:





  • VIRT will increase by 2 GB because the virtual memory address space has been reserved.


  • RES will remain unchanged because no physical memory is used yet.





2. What happens when you “touch” the memory?



The first time you write to a page in the allocated memory:




  • The operating system generates a page fault.


  • It assigns a physical page (RAM) to the virtual address space.


  • That page now counts towards the RES value.




So, RES only grows as you actually use the memory.







3. Can other processes use the unallocated memory (from malloc)?



Yes, absolutely.



Here’s why:




  • When you call malloc, you are only reserving virtual address space in your process. The physical memory (RAM) is not yet allocated.

  • Since the physical RAM is not committed to your process, it remains free for other processes to use.

  • Until you “touch” the memory (write to it), the operating system doesn’t allocate RAM to it.







Analogy to Simplify



Think of the OS as a hotel manager and memory as rooms:





  • Virtual Address Space: You’ve “booked” 2 GB of rooms (via malloc), but the hotel manager only writes your name in the reservation book.


  • Physical Memory: Rooms in the hotel (RAM). The manager doesn’t hand over any rooms to you yet.


  • Touching Memory: When you enter the rooms (write to the memory), the manager actually allocates rooms (physical memory) for you.

  • Until you use the rooms, they are still available for other guests (processes).

  • Your name in the reservation book (VIRT) just says you’ve reserved them if needed.







Quick Example with Code



Here’s a C example to test this:




#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
printf("PID: %d\n", getpid());
printf("Allocating 2 GB using malloc...\n");

char *ptr = malloc(2L * 1024 * 1024 * 1024); // Allocate 2 GB
if (!ptr) {
perror("malloc failed");
return 1;
}

printf("Press Enter to touch memory...\n");
getchar(); // Pause to check top command before touching

for (long i = 0; i < 2L * 1024 * 1024 * 1024; i += 4096) {
ptr[i] = 0; // Touch memory 4KB at a time
}

printf("Memory touched. Press Enter to exit...\n");
getchar(); // Pause to check top command after touching

free(ptr);
return 0;
}






Steps to Test:




  1. Compile and run the program: gcc test.c -o test && ./test


  2. Note the PID and check the top command output:






  • Before touching memory:





    • VIRT increases by 2 GB.


    • RES remains small.








  • After touching memory:




    • RES increases to reflect the actual physical memory used.














Conclusion





  1. malloc without usage:




    • Increases VIRT, but not RES

    • Physical RAM is not allocated.




  2. Other processes:




    • The unallocated memory is still free for other processes to use.

    • Physical RAM is only committed to your process when you "touch" the memory.




  3. Key Point:





    • malloced but unused memory increases VIRT but not RES.

    • Virtual memory reservation (VIRT) does not mean physical memory usage (RES).


    • Physical memory is allocated lazily – only when you access the memory.








Let me know if you'd like further clarification! 🚀

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Tech Interview Series: What Happens When You malloc 2 GB but Don't Use It?

Thematisch verwandte Begriffe: Tech, Interview, Series, What · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-96258 | A vulnerability has been found in onSite internet GmbH Auktion NG Auktio…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick