Tagged: Linux Toggle Comment Threads | Keyboard Shortcuts

  • zhegu 2:31 am on July 28, 2014 Permalink | Reply
    Tags: Linux, malloc, memory, swap space   

    What happens if I keep calling malloc() forever? 

    Today is not a mood-brightening weather in Providence. So I decided to make fun of my computer. Exactly how I did it was by putting malloc within a gigantic loop. Each iteration it would ask OS to allocate a sizable amount of memory. I wondered what would happen. Good question, right?

    Here is how the program looks like:

    int main(int argc, char** argv) {
        const size_t NUM_MALLOCS = LONG_MAX; // 2^31-1, at least.
        long pageSize = sysconf(_SC_PAGE_SIZE);  // Number of bytes to allocate for each malloc() call.
        vector<void*> mallocPtrs;
        for (size_t i = 0; i < NUM_MALLOCS; ++i) {
            void* p = malloc(static_cast<size_t>(pageSize));
            if (p == NULL) {
                // Malloc failed at total size: i * pageSize.
                cleanUp(mallocPtrs);  // Free the allocated memory.
                return 0;
            }  else {
        return 0;

    In theory there’s one outcome I can think of. What the malloc() function does essentially is simply to add some rooms for the process’ data segment to grow, and this increment happens in the heap portion of the virtual memory space. So if we malloc too many times, we might end up having the process’ data segment trespass into the stack portion of virtual memory space (because heap grows low-to-high, and stack grows high-to-low address-wise). This does strike the chord of the notorious segmentation fault, aye?

    In fact, when the program runs, it doesn’t sour up to 100% memory usage and seg-fault immediately afterwards. The memory utilization goes slower and slower towards 80%, and stays at around 81%, and I notice that the swap space starts to utilize some of its free space. 

    It seems that the OS starts to heavily swap cold physical memory pages, both dirty and clean, to the swap space located in my disk. Incredible slow down on my machine as well, as I try to switch terminal screen sessions or to Chrome.

    I kill the processes after a while, before the swap space runs out (which I shouldn’t, because then I’d have been able to see the legendary OOM-killer (out-of-memory killer) come out and kill random processes!).

    Now I notice a huge vacuum in the free memory space:

    sam@butler$ /usr/bin/procinfo
    Memory:        Total        Used        Free     Buffers
    RAM:         4060612      833360     3227252        1016
    Swap:        8388604     2880484     5508120

    And switching to Chrome and terminal is still taking ages… Probably because the pages are still in swap space.

    I start browsing using Chrome, despite the slowness. And I see it slowly regains the speed among switching tabs, because the swapped pages have returned to the physical memory:

    sam@butler$ /usr/bin/procinfo
    Memory:        Total        Used        Free     Buffers
    RAM:         4060612     1395148     2665464        2568
    Swap:        8388604     2625552     5763052
  • zhegu 7:41 pm on January 11, 2013 Permalink | Reply
    Tags: Battery, CPU, , Graphic card, Heating, Intel, Laptop, Linux, Overheat, Radeon, Sony VAIO, Temperature   

    fedora linux overheat problem on sony vaio 

    I have a Sony VAIO S laptop, with Intel i7 CPU and Radeon Graphics. I bought this pretty in the summer of 2012 when I was on a very well-paid internship at @WalmartLabs, CA. At the time I just started the job and was unsatisfied with a given MacBook Pro. My main complaint was the Apple keyboard places the function key at the left bottom corner where the control button usually is. This posts major problem for me since I’m a heavy command-line and vim user, all the muscle memory has already been wired in my both hands. And if you are about to type more than 1000 lines of code, it’s not so pleasant to have to hustle with the typing habit first.

    So I bought a Sony VAIO, and loaded it with Fedora 17. The installation goes through nice and smooth like a charm. However, once I start typing or browsing the web for a few minutes, the fan starts blowing like a turbine and my wrists just can’t even bear the heat on the surface.

    Measure the Temperature

    I notice that it’s not totally my fan’s fault. It’s running up and down with varied speed, responding to hardware heating well enough. The temperature sensor tool that I use is lm_sensors. It gives a temperature reading for my CPU, graphic card and motherboard.

    $ sudo yum install lm_sensors   # Install lm_sensors package
    $ sensors-detect                # Detect available sensors
    $ sensors                       # Get sensor readings

    With light web browsing on Google Chrome, I got

    $ sensors
    Adapter: Virtual device
    temp1: +67.0°C (crit = +97.0°C)
    radeon-pci-0100   # Radeon graphics card
    Adapter: PCI adapter
    temp1: +68.0°C (crit = +100.0°C)
    coretemp-isa-0000   # CPU
    Adapter: ISA adapter
    Physical id 0:+67.0°C (high = +86.0°C, crit = +100.0°C)
    Core 0: +68.0° (high = +86.0°C, crit = +100.0°C)
    Core 1: +66.0°C (high = +86.0°C, crit = +100.0°C)

    68°C is very very hot. Resting on your hands on the keyboard feels like grilling your wrists.

    Turn off the Graphics Card

    My first suspect to the overheating issue is the graphics card. Since Radeon’s graphics cards have long been known for poor linux support and hard to tame under the open source drivers, I am thinking about simply turning off the Radoen graphics card module from the Linux kernel. If it’s the graphics card that causes the heating on mother board, then I should see some dropping of temperature after disabling it.

    Turning off the graphic card can be done in linux with the following shell commands:

    $ modprobe radeon
    $ chown -R $USER:$USER /sys/kernel/debug
    # Turn off the Radeon graphics card
    $ echo OFF > /sys/kernel/debug/vgaswitcheroo/switch

    Now the Radeon graphic card is turned off. It can be verified as:

    $ cat /sys/kernel/debug/vgaswitcheroo/switch
    # External Graphics card is turned off
    0:DIS: :<strong>Off</strong>:0000:01:00.0     
    # Intel graphics is on

    I shut down the computer, let it sit for a while to cool down to room temperature, and reboot again. Immediately after reboot I run the above commands to turn off the graphic card. I immediately notice the fan speed starts to wind down, and after some Chrome web browsing, here is a sample sensor reading:

    $ sensors
    Adapter: Virtual device
    temp1: +47.0°C (crit = +97.0°C)
    Adapter: PCI adapter
    temp1: -128.0°C  # Negative, because it's turned off!
    Adapter: ISA adapter
    Physical id 0: +50.0°C (high = +86.0°C, crit = +100.0°C)
    Core 0: +48.0°C (high = +86.0°C, crit = +100.0°C)
    Core 1: +50.0°C (high = +86.0°C, crit = +100.0°C)

    It’s about 25% reduction on temperature! So the graphic card is really contributing to the overheating. Now I still notice some warming on the surface of the keyboard, but at least it’s not like the surface of the Sahara Desert anymore. Fan is tamed, and the battery is not drained as fast before. Good start.


    Whatever we have achieved above doesn’t repeat after reboot. Before finding a way to further reduce the temperature, I find turning off the graphic card useful enough to automate this task at Fedora startup. Anyway I would always boot into Windows when I need to stream video on Hulu or Youtube. So here is how you do it: put the corresponding set of shell commands (which turns off the graphics card, as listed in the previous section) into /etc/rc.local, which is a system service that will automatically run the shell commands it contains at boot time. Remember to make the rc.local file executable afterwards.

    modprobe radeon
    echo OFF > /sys/kernel/debug/vgaswitcheroo/switch

    The next step is to enable the rc.local service. If you already have /etc/systemd/system/rc-local.service, enable and start it by

    $ sudo systemctl enable rc-local.service
    $ sudo systemctl start rc-local.service
    # Check that rc.local service is indeed enabled and active
    $ sudo systemctl status rc-local.service 
    rc-local.service - /etc/rc.local Compatibility
    Loaded: loaded (/etc/systemd/system/rc-local.service; enabled)
    Active: active (exited) since XXX; XXX ago

    If you don’t have /etc/systemd/system/rc-local.service, here is a good copy of it from my own Fedora, use it at your own risk 🙂

    #  This file is part of systemd.
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU General Public License as published by
    #  the Free Software Foundation; either version 2 of the License, or
    #  (at your option) any later version.
    Description=/etc/rc.local Compatibility
    ExecStart=/etc/rc.local start

    -Sam Zhao

    This solution also seems to prolong the battery life of my laptop quite significantly, from average 2 hours before to 4.5 hours now with web browsing and youtubing. Sweet.

    • sebastian leste 9:59 am on June 23, 2013 Permalink | Reply

      Thanks a Great Job

    • Sello Mathakhoe 6:46 pm on February 27, 2014 Permalink | Reply

      what some great info on your blog about fedora linux overheat problem and I am glad that I got a solution for my laptop. At this time I am using Msi motherboard. Check out this https://www.youtube.com/watch?v=A7miJqWSYRs ,

      Surely Visiting your blog helped me, Awesome stuff!

      Best Regards

    • bestseoservicereview 1:34 pm on March 20, 2014 Permalink | Reply

      This site has got all that I was looking for, thanks, I will bookmark this.Thanks for your helpfull tips,Cheers!!

    • Gokul 9:17 am on March 7, 2015 Permalink | Reply

      I’ve the same overheating and battery problem with my sony vaio e series having Fedora21 installed in it.
      When i try to execute the steps that you’ve mentioned I’m getting the following error.

      [gokul@localhost ~]$ sudo echo OFF > /sys/kernel/debug/vgaswitcheroo/switch
      bash: /sys/kernel/debug/vgaswitcheroo/switch: No such file or directory
      [gokul@localhost ~]$

      My graphics card detail follows,
      [gokul@localhost ~]$ lspci | grep VGA
      01:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Thames [Radeon HD 7550M/7570M/7650M]
      [gokul@localhost ~]$

      Please help me to fix this.. Thanks in advance..

    • service komputer di jakarta 2:52 am on April 9, 2015 Permalink | Reply

      An intriguing discussion is definitely worth comment. I believe that you ought to publish more about this issue, it may not be a taboo subject but typically people do not speak about such issues. To the next! All the best!!

  • zhegu 5:52 pm on April 24, 2012 Permalink | Reply
    Tags: Chinese, , Linux   

    Install Chinese Fonts in Linux Debian 

    Here is the list of packages you need:

    • ttf-arphic-gbsn00lp (AR PL SungtiL GB) //Simplified Chinese
    • ttf-arphic-gkai00mp (AR PL KaitiM GB)  //Simplified Chinese
    • ttf-arphic-bsmi00lp (AR PL Mingti2L Big5)  //Traditional Chinese
    • ttf-arphic-bkai00mp (AR PL KaitiM Big5)    //Traditional Chinese

    To install these packages, run:

    apt-get install ttf-arphic-bkai00mp ttf-arphic-bsmi00lp ttf-arphic-gbsn00lp ttf-arphic-gbsn00lp

    (Source: http://isis.poly.edu/~qiming/chinese-debian-mini-howto.html#Installing_Fonts)

    • Nick 6:30 am on September 17, 2012 Permalink | Reply

      Note that these packages have different names in Wheezy:


      These and other fonts are in a section called ‘Fonts’ in Wheezy.

  • zhegu 7:35 pm on October 28, 2011 Permalink | Reply
    Tags: , Linux   

    Locked dpkg and Freezing Synaptic Manager 

    One possible cause of the freezing synaptic manager is that the /var/lib/dpkg program is currently locked by another process. Remember that the management of packages on Debian or Ubuntu Linux is exclusive.

    If restarting the computing does not fix the problem, try this:

    sudo fuser -vki /var/lib/dpkg/lock

    This command effectively discovers and removes current process(s) that owns the lock to dpkg. Now run a configuration to dpkg:

    sudo dpkg –configure -a

    this command re-configures all the broken packages.

    Finally it is safe to run package upgrade:

    sudo aptitude upgrade

Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc