SQL Server 2012 x64, Total memory allocated 2GB, set to low value to enable us to reproduce "AppDomain unloaded" issues much quicker
Windows Server 2012, Total Memory on Server 16GB
(in production, we have 64GB total memory, which SQL Server can take all of it if needed, this AppDomain unloaded issue still occurs)
SQL Server keeps unloading AppDomain once the allocated 2GB memory is full, which make sense.
what does not make sense is that, why SQL Server does not 're-use' the existing memory (or why does it hang on to the existing AppDomain, not reducing the memory used for existing AppDomain) that it has been using?
or is it simply the way SQLServer works, by unloading AppDomain to reclaim memory ?
the insert query uses the function geometry::STGeomFromText,
there are about 1,000,000 inserts,
which are committed in a transaction of 200 inserts each, and looks like:
INSERT INTO [dbo].[gdb2] ([ogr_geometry], [seg_num], [par_num], [segpar], [par_ind], [prc], [parish], [county], [lac], [shire_name], [feat_name], [loc], [locality], [parcel_typ], [cover_typ], [acc_code], [ca_area_sqm], [shape_length], [shape_area], [globalid]) VALUES (geometry::STGeomFromText('MULTIPOLYGON (((503754.43209999986 6952046.6778,503770.5603 6952043.9964,503790.83380000014 6952040.6264,503810.62700000033 6952037.3364,503824.2081000004 6952035.0799,503825.46609999985 6952014.4771,503805.18599999975 6952017.8472,503786.3114 6952020.9851,503766.46679999959 6952024.2841,503746.6699000001 6952027.5757,503754.43209999986 6952046.6778)))',28356).MakeValid(), 26332, 84, 26332084, 81, 6000, 'OBSOLETE', 'OBSOLETE', 1000, 'CITY', 'AVENUE', 2449, 'SALISBURY', 'R', 'B', 31, 1516.137, 191.870950878815, 1514.92744496079, '{6A0865B9-3D4A-4395-BC78-B2E16CF4E95B}')
Update
Thanks @cody.
Yes 2GB is not enough, but on production, we have 64GB and it is still not enough, that SQLServer is also much busier though.
Increasing the memory to 4GB only postpone the inevitability, AppDomain will be unloaded.
Only setting the memory to 8GB will fix the problem, no more AppDomain issue. However on production SQLServer, we can't get 8GB for our app, the server is much busier, and SQLServer ended up unloading our AppDomain.
Update 2
--script to reproduce the issue
--increase to 40M loop, usually failed at 27M features inserted, for 2GB RAM
Update 3
--this script below does NOT reproduce the issue,
--only ogr2ogr.exe to upload WellKnownText/WKT to SQLServer does it.
USE [test_appDomainUnload]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test_appDomainUnload](
[ogr_geometry] [geometry] NULL,
[id] [bigint] IDENTITY(1,1) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
DECLARE @i bigint = 0
WHILE @i < 40000000
BEGIN
SET @i = @i + 1
BEGIN TRANSACTION
/* do some work */
INSERT INTO [dbo].[test_appDomainUnload] ([ogr_geometry])
VALUES (geometry::STGeomFromText('MULTIPOLYGON (((503754.43209999986 6952046.6778,503770.5603 6952043.9964,503790.83380000014 6952040.6264,503810.62700000033 6952037.3364,503824.2081000004 6952035.0799,503825.46609999985 6952014.4771,503805.18599999975 6952017.8472,503786.3114 6952020.9851,503766.46679999959 6952024.2841,503746.6699000001 6952027.5757,503754.43209999986 6952046.6778)))',28356).MakeValid())
COMMIT
END
Update 4
Attaching the data (FileGDB), ogr2ogr.exe
binaries and ogr.bat
to execute it.
Edit ogr.bat
to set your Database params, and run it.
https://drive.google.com/open?id=0By7mVmnZ1C1oT0lWVTJaZmlabTA
Update 5
To be clear, ogr2ogr.exe
is located at:
we use v1.11, http://github.com/OSGeo/gdal/blob/1.11/gdal/apps/ogr2ogr.cpp,
trunk at http://github.com/OSGeo/gdal/blob/trunk/gdal/apps/ogr2ogr_bin.cpp
runs on another pc, separate to SQLServer machine.
Update 6
Exact Error Message:
time+0: AppDomain 9 (master.sys[runtime].8) is marked for unload due to memory pressure.
time+1: AppDomain 9 (master.sys[runtime].8) unloaded.
time+2: AppDomain 10 (master.sys[runtime].9) created.
It was already AppDomain 9, the previous 8 AppDomains (all master.sys[runtime]
) have been unloaded and recreated.