As the Windows 7 end-of-life is fast approaching, I am testing Ubuntu 18.04.3 LTS on my ancient (2013) laptop. Unfortunately, dual-booting seems nigh-impossible, so I am using a persistent live USB thanks to askubuntu's @sudodus and @C.S.Cameron. I am typing this on Ubuntu and can run LibreOffice and a grand strategy game!

However, I only have 4GB of RAM (which has to double up as video RAM) and the system is grinding to a halt when I reach 100% RAM usage while playing the game. Some of this is probably due to a long-standing memory bug in gnome-software, but I think the game will eventually fill 4GB anyway.

So the next challenge is trying to add some swap. It doesn't need to be available all the time, just when I run games. It's possible to run a swap file on a USB stick, but it's very bad for the stick. So using my internal hard drive would be cheaper and might be faster.

Although I probably can't create a swap partition on my hard drive, it occurred to me that Ubuntu now defaults to using swapfiles. Most HOWTOs assume that swapfile is in an ext2/3/4 partition. But could I, and how would I, create a swapfile in an NTFS partition? The Arch Linux forum has a method for creating an a swapfile on an NTFS USB stick. But that worked on a different distro six years ago and suggests writing a systemd service, which seems ambitious for an Ubuntu beginnner.

In addition, I would be doing this in a NTFS partition that contains hidden files that are required by Windows.

BTW this is not a duplicate of the various answers on how to use a Linux swap partition as a Windows swapfile.

  • 1
    You can create a swap partition on an HDD, even if you boot from a persistant LiveUSB. Any hidden files (including ADS deepinstinct.com/2018/06/12/… ) will stay in their NTFS partition. – K7AAY Nov 11 '19 at 22:05
  • 1
    This is an out of bounds solution; but have you considered adding an mSATA drive? There's a dedicated internal slot for mSATA en.wikipedia.org/wiki/Serial_ATA#Mini-SATA_(mSATA) that would give you the ability to add another drive. I did it with my 2011 T420 and added half a terabyte. amazon.com/… – K7AAY Nov 11 '19 at 22:11
  • 1
    Last but not least, the GNOME Desktop Environment consumes more system resources, esp. memory, than other DEs. Even Canonical says if you have a limited system, consider Lubuntu or Xubuntu. help.ubuntu.com/community/Installation/… – K7AAY Nov 11 '19 at 22:24
  • @K7AAY Thank you for your helpful suggestions. Creating a swap partition seems very risky because of the G570's esoteric partitioning scheme. And unfortunately the G570 doesn't have the mSATA slot that the more expensive 2011 ThinkPads had. – Matthew Nov 12 '19 at 21:09
  • 1
    Oh, bother. Updated the first link to point to G570 specs at Lenovo, you are 100% correct. Maybe you might find a replacement multibay replacing the optical drive to hold a 9.5mm SSD? – K7AAY Nov 12 '19 at 23:40

I'm not quite sure whether a swap file on an NTFS partition is the best solution for you. You may be better off creating an actual swap partition on your hard drive and use that. You could use a tool such as gparted to resize your NTFS partition, reducing it by the size you want your swap file to be, say, 2 GB. Then you could allocate a 2 GB swap partition on the newly freed space on your hard drive.

(Of course that assumes you have at least 2 GB of free space on your NTFS partition. But that's just the same space a swap file would consume as well.)

The reason I'm saying that a swap file on your NTFS partition may not be the right solution is because it may impact performance significantly, for two reasons:

  • Your NTFS partition is not fresh, it's been used for a long time, and your 2 GB swap file will likely be fragmented. Hence, reading and writing to that swap file may be much slower since it is not a contiguous memory region (as opposed to a swap partition). See this excellent answer on Server Fault for some more information.

  • On top of that, the NTFS drivers are rather slow, as mentioned in the Arch Linux forums in the thread that you linked to yourself.

If you are nevertheless determined to create a swap file on your NTFS partition, that's easy. All you have to do is this:

  1. Ensure your NTFS partition is mounted somewhere. For the sake of example, let's assume the mountpoint for your NTFS partition is /media/windows.

  2. Create a swap file with 2 GB on your NTFS partition like so:

dd if=/dev/zero of=/media/windows/swapfile.img bs=1M count=2048
mkswap /media/windows/swapfile.img
  1. Enable the swap with
swapon /media/windows/swapfile.img

That's it really. Those are exactly the instructions from that Arch Linux thread, I merely changed the size of the swap file to 2 GB.

The whole rest of the Arch Linux thread only deals with the issue that the OP wants to have his swap file enabled automatically at boot, so that they don't have to run that swapon command every time after rebooting the system. For that to work, you have to somehow make sure that the NTFS partition is mounted before the swap is enabled, since the swap file is on the NTFS partition, and that can get a bit messy, which is why someone there suggests creating a systemd service to enable the swap after the NTFS partition is mounted.

But if you only want to test how a swap file on your NTFS partition solves the problems with your game, you don't need to worry about that right now. You can just go ahead with the above steps. It simply means that, every time you reboot your system, you have to re-run steps (1) and (3) above, i.e., make sure your NTFS partition is mounted (hint: put it in your /etc/fstab and have it mounted at boot time) and run that swapon command. Note that you only have to perform step (2) once: Once the swap file is created, you don't need to create it anew. Unless you want to change its size.

Then, if that works for you and you're really happy with the results and you can play your game and one day you're annoyed that you have to run that swapon command every time you reboot your system, then you can worry about writing a systemd service to run that swapon command every time automatically at boot. And then, if you have trouble with that, you're welcome to ask another specific question about that problem here. Or perhaps, by then you will like Ubuntu so much that you decide to get rid of Windows 7 and install Ubuntu for good. ;)

  • 1
    Indeed, I should have mentioned that you need to run those commands with sudo (i.e., as root). Actually, the dd and mkswap commands could in principle be run by a user if the NTFS partition was mounted with appropriate uid/gid options (see mount(8)), but anyway you only need to run those commands once. For swapon, sure, only root can tell the system to use this or that swap, not just any user (and that's good), but as you said, this is of no concern in a single-user system. I'm happy that it helped! – Malte Skoruppa Nov 15 '19 at 15:31
  • 1
    Note: You may obtain a better performance from your swap file if you defragment your NTFS partition (from Windows) first, then create the swap file (from Linux) right after the defragmentation. This way at least your swap file will not be fragmented. The NTFS drivers are still slow, but at least the swap file will be a contiguous memory region. I already said this, but you will obtain an even better performance if you create a small swap partition instead of a file. :) – Malte Skoruppa Nov 15 '19 at 15:33
  • 1
    Thank you for the extra advice. That's a good point about defragmentation. TBH the game actually runs about 5 times faster from the Ubuntu USB than from the internal HDD on Windows, so I it's already an improvement. And gnome-system-monitor makes it easy to see that I'm only using <=150MB of swap anyway. BTW the Bash script question is now up (and you get credit!) – Matthew Nov 15 '19 at 15:42
  • 1
    @Ramesh-X The drawback of using an SSD for swapping is that SSDs have a more limited lifespan than HDDs. Everytime the SSD is written to, the SSD is worn out a little bit more. Hence, with respect to your SSD's lifespan, it makes no difference whatsoever whether you use a swap file or a swap partition - your SSD is worn out in the same way. On the plus side, though, an SSD is much faster than an HDD, and you want your swap to be fast, so IMO it's actually good to use your SSD for swapping. Also, SSDs nowadays have much better lifecycles than a few years ago, so I wouldn't worry about it. – Malte Skoruppa Apr 2 '20 at 18:36
  • 1
    Also see here for a much more detailed answer concerning the use of SSDs for swapping: askubuntu.com/q/652337/223688. – Malte Skoruppa Apr 2 '20 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.