Index: trunk/src/org/openstreetmap/josm/gui/MainInitialization.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/MainInitialization.java	(revision 16320)
+++ trunk/src/org/openstreetmap/josm/gui/MainInitialization.java	(revision 16321)
@@ -39,5 +39,4 @@
 import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
 import org.openstreetmap.josm.tools.PlatformManager;
-import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
 import org.openstreetmap.josm.tools.Shortcut;
 import org.openstreetmap.josm.tools.Tag2Link;
@@ -82,7 +81,5 @@
                 MainApplication.registerActionShortcut(MainApplication.menu.help,
                         Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.DIRECT));
-            }),
-            // This needs to be done before RightAndLefthandTraffic::initialize is called
-            new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize)
+            })
         );
     }
@@ -114,6 +111,6 @@
                     }
                 }),
-            new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize),
-            new InitializationTask(tr("Initializing numbering format"), () -> {
+            new InitializationTask(tr("Initializing internal boundaries data"), () -> {
+                Territories.initialize();
                 if (Config.getPref().getBoolean("override.numbering.format", true)) {
                     I18n.initializeNumberingFormat();
Index: trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java	(revision 16320)
+++ trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java	(revision 16321)
@@ -44,5 +44,5 @@
 import org.openstreetmap.josm.tools.OptionParser.OptionCount;
 import org.openstreetmap.josm.tools.OptionParser.OptionParseException;
-import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
+import org.openstreetmap.josm.tools.Territories;
 
 /**
@@ -439,5 +439,5 @@
         ProjectionRegistry.setProjection(Projections.getProjectionByCode(projCode.toUpperCase(Locale.US)));
 
-        RightAndLefthandTraffic.initialize();
+        Territories.initialize();
     }
 
Index: trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java	(revision 16320)
+++ trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java	(revision 16321)
@@ -90,4 +90,8 @@
     }
 
+    /**
+     * Requires {@link Logging#isDebugEnabled()}, otherwise dataset is unloaded
+     * @see Territories#initializeInternalData()
+     */
     private static final class EditBoundariesAction extends AbstractAction {
         EditBoundariesAction() {
@@ -391,6 +395,8 @@
         menu.addSeparator();
         menu.add(getProfileMenu());
-        menu.addSeparator();
-        menu.add(new EditBoundariesAction());
+        if (Logging.isDebugEnabled()) {
+            menu.addSeparator();
+            menu.add(new EditBoundariesAction());
+        }
         menu.addSeparator();
         menu.add(new ResetPreferencesAction());
Index: trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java
===================================================================
--- trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java	(revision 16320)
+++ trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java	(revision 16321)
@@ -2,5 +2,4 @@
 package org.openstreetmap.josm.tools;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -8,5 +7,4 @@
 
 import org.openstreetmap.josm.data.coor.LatLon;
-import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
 import org.openstreetmap.josm.data.osm.Relation;
@@ -43,21 +41,19 @@
     /**
      * Initializes Right and lefthand traffic data.
+     * @param geoProperty the property containing the traffic data
      * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
      */
-    public static synchronized void initialize() {
-        rlCache = new GeoPropertyIndex<>(computeLeftDrivingBoundaries(), 24);
+    static synchronized void initialize(DefaultGeoProperty geoProperty) {
+        rlCache = new GeoPropertyIndex<>(geoProperty, 24);
     }
 
-    private static DefaultGeoProperty computeLeftDrivingBoundaries() {
-        Collection<Way> ways = new ArrayList<>();
+    static void appendLeftDrivingBoundaries(OsmPrimitive osm, Collection<Way> ways) {
         // Find all outer ways of left-driving countries. Many of them are adjacent (African and Asian states)
-        DataSet data = Territories.getDataSet();
-        for (Way w : data.getWays()) {
-            if (LEFT.equals(w.get(DRIVING_SIDE))) {
+        if (LEFT.equals(osm.get(DRIVING_SIDE))) {
+            if (osm instanceof Way) {
+                Way w = (Way) osm;
                 addWayIfNotInner(ways, w);
-            }
-        }
-        for (Relation r : data.getRelations()) {
-            if (r.isMultipolygon() && LEFT.equals(r.get(DRIVING_SIDE))) {
+            } else if (osm instanceof Relation && osm.isMultipolygon()) {
+                Relation r = (Relation) osm;
                 for (RelationMember rm : r.getMembers()) {
                     if (rm.isWay() && "outer".equals(rm.getRole()) && !RIGHT.equals(rm.getMember().get(DRIVING_SIDE))) {
@@ -67,6 +63,4 @@
             }
         }
-        // Combine adjacent countries into a single polygon
-        return new DefaultGeoProperty(ways);
     }
 
Index: trunk/src/org/openstreetmap/josm/tools/Territories.java
===================================================================
--- trunk/src/org/openstreetmap/josm/tools/Territories.java	(revision 16320)
+++ trunk/src/org/openstreetmap/josm/tools/Territories.java	(revision 16321)
@@ -32,8 +32,10 @@
 import org.openstreetmap.josm.data.coor.LatLon;
 import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
 import org.openstreetmap.josm.data.osm.Relation;
 import org.openstreetmap.josm.data.osm.TagMap;
 import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
 import org.openstreetmap.josm.io.CachedFile;
 import org.openstreetmap.josm.io.IllegalDataException;
@@ -114,12 +116,4 @@
 
     /**
-     * Returns a copy of the territories dataset.
-     * @return a copy of the territories dataset
-     */
-    public static synchronized DataSet getDataSet() {
-        return new DataSet(dataSet);
-    }
-
-    /**
      * Initializes territories.
      * TODO: Synchronization can be refined inside the {@link GeoPropertyIndex} as most look-ups are read-only.
@@ -138,10 +132,12 @@
         taginfoCache = new TreeMap<>();
         customTagsCache = new TreeMap<>();
+        Collection<Way> traffic = new ArrayList<>();
         try (CachedFile cf = new CachedFile("resource://data/" + FILENAME);
                 InputStream is = cf.getInputStream()) {
             dataSet = OsmReader.parseDataSet(is, null);
-            Collection<OsmPrimitive> candidates = new ArrayList<>(dataSet.getWays());
-            candidates.addAll(dataSet.getRelations());
-            for (OsmPrimitive osm : candidates) {
+            for (OsmPrimitive osm : dataSet.allPrimitives()) {
+                if (osm instanceof Node) {
+                    continue;
+                }
                 String iso1 = osm.get(ISO3166_1);
                 String iso2 = osm.get(ISO3166_2);
@@ -165,7 +161,17 @@
                     }
                 }
-            }
+                RightAndLefthandTraffic.appendLeftDrivingBoundaries(osm, traffic);
+            }
+            RightAndLefthandTraffic.initialize(new DefaultGeoProperty(traffic));
         } catch (IOException | IllegalDataException ex) {
             throw new JosmRuntimeException(ex);
+        } finally {
+            MultipolygonCache.getInstance().clear(dataSet);
+            if (!Logging.isDebugEnabled()) {
+                // unset dataSet to save memory, see #18907
+                dataSet = null;
+            } else {
+                Logging.debug("Retaining {0} to allow editing via advanced preferences", FILENAME);
+            }
         }
     }
