Cannot Access the WiFi Internet on Mac with: Cannot Allocate Memory

I have been noticing more and more frequently then when I switch between WiFi connections or switch between wired and WiFi connections that sometimes my WiFi connection would show that it was online, but I was unable to access anything on the LAN or WAN. I have experienced similar issues when DNS is not working properly, so my first test is to validate DNS is working. With this recent issue my test always returned:

$ > nslookup cnn.com
/SourceCache/bind9/bind9-45.101/bind9/lib/isc/unix/socket.c:1890: internal_send: 192.168.1.1#53: Cannot allocate memory

So what is going on and how can you fix the issue?
mac-wifi

Well a few bullet points to start:

  • Based on the error message, it would appear that DNS is not the issue. Of course, this can be confirmed by changing the configured DNS servers and testing again — the results will be the same.
  • Disconnecting and reconnecting the WiFi does not make a difference. However going from WiFi back to a wired connection does get you back on the Internet. Switching back from wired to WiFi will bring back the same issue.
  • If you pull a Windows and restart the issue goes away temporarily.

In short, the issue is known and there are some workaround, by how do you fix the issue when it occurs? Well, a quick Google search on the error messages brings up a ton of forums with people experiencing the same issue. Many suggest changing the proxy settings on the Mac to automatic, but in my case that had no effect.
After spending quite a bit of time looking for another answer, I came across someone who suggested deleting the route to the gateway of the WiFi you are connecting to. In short, when you connect to WiFi, you connect to a router. That router is responsible for routing your traffic. It does this by acting as a gateway. It either knows where to send your traffic because it controls IP or it sends the traffic upstream.
I found this suggesting interesting because I had noted that sometimes switching the WiFi from one access point to another sometimes fixed the issue without a restart. I decided to give this a try. First up, I had to find out what my default gateway was:

$ > netstat -rn
Routing tables
Internet:
Destination Gateway Flags Refs Use Netif Expire
default 192.168.1.1 UGSc 34 0 en0
65.116.132.233 192.168.1.1 UGHCS 0 0 en0
127 127.0.0.1 UCS 0 0 lo0
127.0.0.1 127.0.0.1 UH 10 82789 lo0
169.254 link#4 UCS 0 0 en0
192.168.1 link#4 UCS 5 0 en0
192.168.1.1 link#4 UHCS 1 0 en0
192.168.1.1/32 link#4 UCS 0 0 en0
192.168.1.230/32 link#4 UCS 0 0 en0
192.168.1.255 ff:ff:ff:ff:ff:ff UHLWbI 0 4 en0
192.168.4.1 link#4 UHCS 0 0 en0
192.168.5.211 link#4 UHCS 0 0 en0
192.168.248.1 link#4 UHCS 0 0 en0

Based on this, I know my default gateway is 192.168.1.1. Next, to delete the route:

$ > sudo route delete -host 192.168.1.1

Finally, disable and then enable WiFi. After completing these steps, I was back online! This meant I had a fix, but it was not permanent or automatic. To make it permanent and automatic, I needed to write-up a little script:

nslookup cnn.com | grep 'Cannot allocate memory' >/dev/null 2>/dev/null
if [ "$?" != "0" ]; then
sudo route delete -host $(netstat -rn | grep ^default | awk '{print $2}')
networksetup -setairportpower en0 off
networksetup -setairportpower en0 on
fi

You could then run the above script on a cronjob at an interval of your choosing. Now, based on my testing, this issue only happens when joining an access point so you could check for WiFi changes prior to checking for and fixing this known issue:

WIFI=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep SSID | tail -n 1 | awk '{print $2}')
if [ "$WIFI" != "$WIFI2" ]; then
nslookup cnn.com | grep 'Cannot allocate memory' >/dev/null 2>/dev/null
if [ "$?" != "0" ]; then
sudo route delete -host $(netstat -rn | grep ^default | awk '{print $2}')
networksetup -setairportpower en0 off
networksetup -setairportpower en0 on
fi
WIFI2=$WIFI
fi

You could choose to run this alternative script on a while true loop with a sleep time of your choosing. I hope this helps someone is it was quite a pain for me!

© 2015, Steve Flanders. All rights reserved.

2 comments on “Cannot Access the WiFi Internet on Mac with: Cannot Allocate Memory

pavansura says:

Thanks for sharing this Steve. Saved my mac reboot time which is what I was doing so far..:)

Glad it helped!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top