DEV Community

Pacharapol Withayasakpunt
Pacharapol Withayasakpunt

Posted on • Originally published at polv.cc

WSL vs plain old VirtualBox

So, I have been using WSL2 on Windows 10 for a while; and I have made a few complaints. The latest one is.

Another major complaint is

WSL 2 consumes massive amounts of RAM and doesn't return it #4166

  • Your Windows build number: 18917

  • What's wrong / what should be happening instead: WSL 2 starts using huge amounts of RAM after a while, just using it like normal. At the moment I'm using phpstorm, and did a dump/load of a database. Vmmem is using 7 GB of my 16 GB of RAM and not returning any, even though Ubuntu is actually using much less. I have seen it grow until nearly 100% of my system memory is in use, and it will not release it until I shut down the WSL 2 VM.

This may or may not be related to #4159

corey@Corey-Laptop:/mnt/c/WINDOWS/system32$ vmstat -s
     15235516 K total memory
       920348 K used memory
      1886048 K active memory
      6434312 K inactive memory
      6606548 K free memory
        76280 K buffer memory
      7632340 K swap cache
            0 K total swap
            0 K used swap
            0 K free swap
       163729 non-nice user cpu ticks
          298 nice user cpu ticks
        13177 system cpu ticks
     68988300 idle cpu ticks
         8962 IO-wait cpu ticks
            0 IRQ cpu ticks
        10022 softirq cpu ticks
            0 stolen cpu ticks
      1481417 pages paged in
      6792976 pages paged out
            0 pages swapped in
            0 pages swapped out
      1079177 interrupts
      5131981 CPU context switches
   1560599814 boot time
         8772 forks

Luckily, I upgraded my laptop from 8GB to 16GB in advance. It uses 8GB RAM at baseline. However, there is no real performance delay.

RAM used

Compared to my real Linux-first laptop (Ubuntu GNOME 3), which uses less than 1GB RAM (total 8GB RAM - yes, that triple-booted 8-year-old MacBook Pro).

So, I decided to visit my old friend, VirtualBox.

VirtualBox advantages (and cons)

  • Graphics and input method engines are all decent and reliable. (Forgot to say that sound doesn't work in WSL2's X410, but does work in VirtualBox.)
  • True sandboxing. Better security.
    • Of course, a big pro/con of WSL2, is that you can access filesystem, bidirectionally to-and-from Windows. (to-WSL: SSH; from-WSL: network drive)

WSL2 does have some unique cons.

  • Web development in WSL2, when opening a server port - the port increases by one in Windows, e.g. 8080 => 8081. None of these shit in VirtualBox.
  • systemd, and perhaps many other default daemons, does not run at all in WSL2. You cannot always expect things to work the same way as plain old Linux.
  • GUI, even with the best xrdp or X410 (or VcXsrv), throws error often. Input method engines do not work.

Of course, VirtualBox comes with the same old cons.

  • Harder to set up to be performant. Have to manually allocate not only RAM and hard disk space; but also CPU's and even BIOS settings sometimes.
    • Everything is automated and defaulted in WSL2. You don't have to set up anything to be performant. (You might want to limit RAM usage, though.)
  • I still cannot figure a way to access WSL2 from VirtualBox. Network drives cannot be accessed from VirtualBox shared folders.

Conclusions

Why not install both?

I am starting to think that, if your PC is powerful enough, use VirtualBox first. But do install WSL2, for the sake of Docker.

Also, consider working at the level of Natively Windows.

Latest comments (5)

Collapse
 
amritanshupandey profile image
Amritanshu Pandey

If automation and ease of setting up is a concern, you can also look at Multipass (multipass.run/).

It shows creating VMs easily using pre installed images, and it supports cloud-init for configuring if desired.

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Thanks for your suggestion, and just tested.

  • Multipass seems to need either VirtualBox or Hyper-V. However, Hyper-V isn't available in my Windows 10 Home Edition
  • Multipass distro? It's an CLI-based distro only; no desktop environment. I currently have most problems with GUI.
  • VirtualBox can also install mini.iso, if I really needed to. Also, VirtualBox's GUI makes to easier to manage.

If I used Windows 10 Ultimate Edition or something, I might change my mind. But, it doesn't make sense for me to purge genuine Windows 10 that came with the laptop, just to install a more complete version of Windows.

Collapse
 
ivan_jrmc profile image
Ivan Jeremic

Never had this issue with ports

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

It's an issue that is obvious to me. It usually hurts nothing, except for gin-swagger.

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "runtime"
    "strconv"
    "strings"

    "github.com/gin-gonic/gin"
    swaggerFiles "github.com/swaggo/files"
    ginSwagger "github.com/swaggo/gin-swagger"

    _ "${MY_GO_MOD}/docs"
)

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host petstore.swagger.io
// @BasePath /v2
func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    r := gin.New()

    swaggerURL := fmt.Sprintf("http://localhost:%s/swagger/doc.json", port)
    if runtime.GOOS == "linux" {
        if data, err := ioutil.ReadFile("/proc/version"); err == nil && strings.Contains(string(data), "microsoft") {
            if p, err := strconv.Atoi(port); err == nil {
                swaggerURL = fmt.Sprintf("http://localhost:%d/swagger/doc.json", p+1)
            }
        }
    }

    url := ginSwagger.URL(swaggerURL)
    r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

    log.Printf("Server running at http://localhost:%s", port)
    r.Run(":" + port)
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ug02fast profile image
Arthur Zhuk

WSL is still in the prototype phase. It's fun for a while until you get hit with the obnoxious memory drain. +1 for VBox. Not a pleasant experience setting up dev environment w\ WSL.