Browse Source

Garbage collection modifications

- In mobile library, do a garbage collection
  in Start to reclaim memory when toggling.

- Call defaultGarbageCollection in all cases
  except where intending to switch automatic
  garbage collection frequency.
Rod Hynes 8 years ago
parent
commit
8eb2d265c1
3 changed files with 15 additions and 5 deletions
  1. 7 0
      MobileLibrary/psi/psi.go
  2. 4 5
      psiphon/controller.go
  3. 4 0
      psiphon/utils.go

+ 7 - 0
MobileLibrary/psi/psi.go

@@ -84,6 +84,13 @@ func Start(
 		return fmt.Errorf("already started")
 	}
 
+	// Clients may toggle Stop/Start immediately to apply new config settings
+	// such as EgressRegion or Authorizations. When this restart is within the
+	// same process and in a memory contrained environment, it is useful to
+	// force garbage collection here to reclaim memory used by the previous
+	// Controller.
+	psiphon.DoGarbageCollection()
+
 	// Wrap the provider in a layer that locks a mutex before calling a provider function.
 	// The the provider callbacks are Java/Obj-C via gomobile, they are cgo calls that
 	// can cause OS threads to be spawned. The mutex prevents many calling goroutines from

+ 4 - 5
psiphon/controller.go

@@ -683,7 +683,7 @@ loop:
 				// Clear the reference to this discarded tunnel and immediately run
 				// a garbage collection to reclaim its memory.
 				connectedTunnel = nil
-				aggressiveGarbageCollection()
+				defaultGarbageCollection()
 
 				// Skip the rest of this case
 				break
@@ -1392,7 +1392,7 @@ loop:
 
 		// ConnectTunnel will allocate significant memory, so first attempt to
 		// reclaim as much as possible.
-		aggressiveGarbageCollection()
+		defaultGarbageCollection()
 
 		// Select the tunnel protocol. Unless config.TunnelProtocol is set, the
 		// selection will be made at random from protocols supported by the
@@ -1484,8 +1484,7 @@ loop:
 		if err != nil {
 			tunnel = nil
 		}
-
-		aggressiveGarbageCollection()
+		defaultGarbageCollection()
 
 		if err != nil {
 
@@ -1517,7 +1516,7 @@ loop:
 			// Clear the reference to this discarded tunnel and immediately run
 			// a garbage collection to reclaim its memory.
 			tunnel = nil
-			aggressiveGarbageCollection()
+			defaultGarbageCollection()
 		}
 
 		// Unblock other candidates only after delivering when

+ 4 - 0
psiphon/utils.go

@@ -227,3 +227,7 @@ func standardGarbageCollection() {
 func defaultGarbageCollection() {
 	debug.FreeOSMemory()
 }
+
+func DoGarbageCollection() {
+	defaultGarbageCollection()
+}