Thursday, May 16, 2013

Taming the Chrome Memory Pig

As I wrote in my last post, I've had problems with memory swapping on my Ubuntu system. Swapping is when programs and data are taken out of RAM and put on disk. Systems do this so they won't run out of RAM. Ubuntu, however, ships with a fairly aggressive swapping strategy.

The way I fixed the problem was to reduce the swappiness configurable in Ubuntu but, as I was analyzing that, I discovered that both Xorg and Chrome were eating a great deal of memory. As far as I can tell, Xorg bloats due to applications not releasing display memory properly. I figure there's not much I can do about that.

Chrome, however, was another matter. I found a Chrome plugin called OneTab. This product claims to reduce Chrome memory usage by 95%. It accomplishes this by collapsing all the open browser tabs down to one filled with links which, when clicked, restore the tab with no data loss.

It actually seems to work. The only problem is that restored windows don't retain their browser history so you can't use the back button to return to a previous screen after restoring a window. Still, when Chrome has your system crawling, OneTab is a good alternative.

Tuesday, May 14, 2013

Ubuntu Swappiness

My desktop has been running slow for a while. Granted I have a PoC computuer with only 4GB of RAM but it should suffice to run Ubuntu. The disk seems to grind a lot which usually indicates a lot of swapping. Since the wife and I use different browsers I have to assume one is swapped out over time. This seems odd since the system monitor shows swap space being used even though I have plenty of RAM left.

Enter the swappiness configurable. Here's a cut and past from the Swap FAQ.

What is swappiness and how do I change it?


The swappiness parameter controls the tendency of the kernel to move processes out of physical memory and onto the swap disk. Because disks are much slower than RAM, this can lead to slower response times for system and applications if processes are too aggressively moved out of memory.
  • swappiness can have a value of between 0 and 100
  • swappiness=0 tells the kernel to avoid swapping processes out of physical memory for as long as possible
  • swappiness=100 tells the kernel to aggressively swap processes out of physical memory and move them to swap cache
The default setting in Ubuntu is swappiness=60. Reducing the default value of swappiness will probably improve overall performance for a typical Ubuntu desktop installation. A value of swappiness=10 is recommended, but feel free to experiment. Note: Ubuntu server installations have different performance requirements to desktop systems, and the default value of 60 is likely more suitable.
To check the swappiness value
cat /proc/sys/vm/swappiness

To change the swappiness value A temporary change (lost on reboot) with a swappiness value of 10 can be made with
sudo sysctl vm.swappiness=10

To make a change permanent, edit the configuration file with your favorite editor:
gksudo gedit /etc/sysctl.conf

Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:
vm.swappiness=10

Save the file and reboot.

I'll try a swappiness of 10 and report back here in a few days if performance improves. The instant I changed it the disk stopped grinding so I am hopeful this will fix my problem.



Sunday, May 5, 2013

Setting up Ubuntu as a Media Server for Xbox and Android Devices

Recently we cut the cord with cable, relying solely on the internet for our entertainment. The reasons were many but not going deeper in the financial hole each month was a strong motivator. Also, our Wii system broke so we needed a replacement media console for Netflix and Hulu. My son wanted an Xbox we decided to go down that path.

The Xbox is a beefier gaming console and now that Zack is older, he wants to play more of the shoot-em-up games which the Wii doesn't support well. The Wii also does poorly with flight simulators, which Zack loves, so I decided to bite the bullet and spend the $300 for a new system.

I was pleasantly surprised to find a pretty sweet deal on new Xboxs. You can get one with 256 GB of memory for $99 if you sign up for 2 years of Xbox live. Since I was going to need the live account anyway, it seemed like a pretty cheap entry point.

What we had:
AT&T cable with a 12 MB internet pipe and phone, Netflix, Hulu, Amazon ~ $200/month

What we have now
25 MB internet pipe, phone, Netflix, Hulu, Xbox Live ~ $80/month

So we're saving around $120 a month. Over a year that adds up.

Before I start on the technical details I have to add a caveat: the wife isn't happy with the number of steps it takes to watch her shows. Some of them I have to find on either Amazon, Hulu, or Neflix. Others I find them on YouTube or other media sources. Still, we only watch a few shows each week so I don't consider this a burden. I think my wife feels differently.

Also, if you aren't going to use your PC as a media server that plays content you've stored there, then you really don't need to read much of this article. Everything I've listed above is well documented and pretty easy to set up. If, however, you want to use your computer as a way to serve up music, photos, and videos then read on.

Also, this is one area where windows is probably a better solution than Ubuntu. I have Ubuntu doing well but the Xbox is a Microsoft product and thus integrates with Microsoft windows really well. I've read more than one post stating people basically gave up and bought a cheap computer to sit next to their gaming console.

So, for you Ubuntu and Mac users, read on.

Recommendations:
  • This assumes you already have a broadband internet connection. Most plans these days will do but the ideal size of your internet pipe depends on how many machines you're going to connect. 12 MB download speed should be sufficient for anything. 
  • For the best experience, I suggest an HDTV using an HDMI cable for connection between the Xbox and the TV. 
Details
  1. Go to the $99 Xbox page - you'll have to sign up for an Xbox Live account for $15 a month then take the coupon to one of the participating stores (Microsoft, Best Buy, GameStop, Toys R Us, or Walmart) to redeem the coupon. You'll have a choice between a larger disk drive and a Kinex system. I recommend more drive space since the 4GB that comes with the Kinex isn't enough to store much.
  2. Purchase your system and install it at home. I would leave it outside of any cabinet, this system runs a hot.
  3. Set up your Amazon, Hulu, and Netflix accounts - its fairly simple. ( you can stop here if you aren't setting up your PC as a media server)
  4. Go to the PS3 Media Server Download page. You'll find links for Windows, Mac, and Linux. 
  5. For Linux, you'll see a pms-generic-linux-unix*.tgz file. Download this and extract the contents to a directory of your choosing.
  6. You have a choice of running the server by hand or setting it up as some sort of service. I still run it by hand but created a script on my desktop to run it. To run it the first time open a terminal window, cd to your extract director and run ./PMS.sh. You should see the window below. 
  7. You'll need to go to the Navigation/Share Settings tab and tell the PS3 Media Server what directories it can access.
  8. Add your Video, Picture, and Music directories here.
  9. I've found the transcoding video media streams doesn't seem to work well from PMS. My devices time out before my POC computer can serve the first of the stream. My advice is to only stream videos your device (Xbox or Android) that they can consume natively. Therefore, go to the transcoding tab and add mp4 and mpv to the 'skip transcode' box. 
  10. I'm not sure if this is needed but I would Quit the PMS and then start it back up again. 
  11. Now go to your Xbox and access the System Media Player under TV and Video. You should see your PMS server available. Now navigate to your content and enjoy!
Diving Deeper

Now for some of the fiddly bits. The MKV format is popular for downloadable internet content but Xbox doesn't support it and, if I read the Xbox support site correctly, they have no intention of supporting it.  A product called HandBrake solves the problem.

With HandBrake I can convert the mkv files into mp4 format which the Xbox reads well. To install on Ubuntu run the following commands in a terminal window
sudo add-apt-repository ppa:stebbins/handbrake-releases
sudo apt-get update
sudo apt-get install handbrake-gtk
This link talks about handbrake no longer supporting xbox and ps3 but other say it does
I installed it and created a m4v format ( basically an mp4 with apple compatibility crap) and it worked fine.

Other media streamers I've experimented with are MediaTomb and minidlna. Both are available from the Ubuntu store (for free) and both work well on my Android device. Xbox won't even see MediaTomb, however and minidlna sort of works for music.

For minidlna, you'll want to follow the config instructions at https://help.ubuntu.com/community/MiniDLNA
On extra thing I needed to do was manually create the /var/cache/minidlna directory then grant global permissions to is since it's used by everyone. The commands are
mkdir /var/cache/minidlna
chmod a+rw /var/cache/minidlna
Android

So this is one of those happy finds. I wanted a way to stream my PS3 Media Server to my phone. I found a package called Skifta available from the Google Play and Amazon store. Not only does it let me watch files served from any of the media servers I've listed in the article, it also share my phones media to anyone on my local network. So now I can watch movies on my phone on my Xbox. I use it on both my Kindle and Samsung.

I'm still experimenting with mkv formats on android. The best option so far seems to be creating either an mp4 or avi out of the mkv. A product called avidemux solves the problem. Just select the file type of either avi or mp4 when saving. It takes a while. After that you have video content your android can read.

I'm not happy that avidemux can't create mp4s or avis that xbox can read. I'm sure with a little more digging I could find the proper settings but this will do for now.

Good luck and happy viewing. 

Links

PS3 Media Server - http://www.ps3mediaserver.org/