Skip to content

Commit 0c4dc19

Browse files
committed
added close method to array cache to prevent memory leaks
1 parent 7f35119 commit 0c4dc19

File tree

5 files changed

+61
-54
lines changed

5 files changed

+61
-54
lines changed

core/src/main/java/com/bc/fiduceo/reader/ArrayCache.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public ArrayCache(NetcdfFile netcdfFile) {
4848
variableFinder = netcdfFile::findVariable;
4949
}
5050

51+
public void clear() {
52+
cache.clear();
53+
scaledCache.clear();
54+
injectedVariables.clear();
55+
}
56+
5157
// package access for testing only tb 2016-04-14
5258
static String createGroupedName(String groupName, String variableName) {
5359
return groupName + "_" + variableName;

core/src/main/java/com/bc/fiduceo/reader/netcdf/NetCDFReader.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public void open(File file) throws IOException {
2929

3030
@Override
3131
public void close() throws IOException {
32-
arrayCache = null;
32+
if (arrayCache != null) {
33+
arrayCache.clear();
34+
arrayCache = null;
35+
}
3336

3437
if (netcdfFile != null) {
3538
netcdfFile.close();

core/src/test/java/com/bc/fiduceo/TestUtil.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -129,41 +129,41 @@ public static void writeSystemConfig(File configDir) throws IOException {
129129

130130
public static void writeSystemConfig(File configDir, File archiveRoot) throws IOException {
131131
final String systemConfigXML = "<system-config>" +
132-
" <geometry-library name = \"S2\" />" +
133-
" <archive>" +
134-
" <root-path>" +
135-
" " + archiveRoot.getAbsolutePath() +
136-
" </root-path>" +
137-
" <rule sensors = \"drifter-sst, ship-sst, gtmba-sst, radiometer-sst, argo-sst, xbt-sst, mbt-sst, ctd-sst, animal-sst, bottle-sst\">" +
138-
" insitu/SENSOR/VERSION" +
139-
" </rule>" +
140-
" <rule sensors = \"animal-sirds, argo-sirds, argo_surf-sirds, bottle-sirds, ctd-sirds, drifter-sirds, drifter_cmems-sirds, gtmba-sirds, mbt-sirds, mooring-sirds, ship-sirds, xbt-sirds\">" +
141-
" insitu/sirds/VERSION" +
142-
" </rule>" +
143-
" <rule sensors=\"ocean-rain-sst\">\n" +
144-
" insitu/SENSOR/VERSION\n" +
145-
" </rule>" +
146-
" <rule sensors=\"gruan-uleic\">\n" +
147-
" insitu/SENSOR/VERSION\n" +
148-
" </rule>" +
149-
" <rule sensors = \"iasi-ma, iasi-mb\">" +
150-
" SENSOR/VERSION/YEAR/MONTH" +
151-
" </rule>" +
152-
" <rule sensors = \"mod06-te, myd06-aq, mod35-te, myd35-aq, airs-aq\">" +
153-
" SENSOR/VERSION/YEAR/DAY_OF_YEAR" +
154-
" </rule>" +
155-
" <rule sensors = \"miras-smos-CDF3TD, miras-smos-CDF3TA\">" +
156-
" SENSOR/VERSION/YEAR/DAY_OF_YEAR" +
157-
" </rule>" +
158-
" <rule sensors = \"ANTXXXI-sic-cci, DMISIC0-sic-cci, DTUSIC1-sic-cci\">" +
159-
" insitu/sic-cci/SENSOR/VERSION" +
160-
" </rule>" +
161-
" </archive>" +
162-
" <temp-directory>" +
163-
" " + TestUtil.getTestDir().getAbsolutePath() +
164-
" </temp-directory>" +
165-
" <reader-cache-size>12</reader-cache-size>" +
166-
"</system-config>";
132+
" <geometry-library name = \"S2\" />" +
133+
" <archive>" +
134+
" <root-path>" +
135+
" " + archiveRoot.getAbsolutePath() +
136+
" </root-path>" +
137+
" <rule sensors = \"drifter-sst, ship-sst, gtmba-sst, radiometer-sst, argo-sst, xbt-sst, mbt-sst, ctd-sst, animal-sst, bottle-sst\">" +
138+
" insitu/SENSOR/VERSION" +
139+
" </rule>" +
140+
" <rule sensors = \"animal-sirds, argo-sirds, argo_surf-sirds, bottle-sirds, ctd-sirds, drifter-sirds, drifter_cmems-sirds, gtmba-sirds, mbt-sirds, mooring-sirds, ship-sirds, xbt-sirds\">" +
141+
" insitu/sirds/VERSION" +
142+
" </rule>" +
143+
" <rule sensors=\"ocean-rain-sst\">\n" +
144+
" insitu/SENSOR/VERSION\n" +
145+
" </rule>" +
146+
" <rule sensors=\"gruan-uleic\">\n" +
147+
" insitu/SENSOR/VERSION\n" +
148+
" </rule>" +
149+
" <rule sensors = \"iasi-ma, iasi-mb\">" +
150+
" SENSOR/VERSION/YEAR/MONTH" +
151+
" </rule>" +
152+
" <rule sensors = \"mod06-te, myd06-aq, mod35-te, myd35-aq, airs-aq\">" +
153+
" SENSOR/VERSION/YEAR/DAY_OF_YEAR" +
154+
" </rule>" +
155+
" <rule sensors = \"miras-smos-CDF3TD, miras-smos-CDF3TA\">" +
156+
" SENSOR/VERSION/YEAR/DAY_OF_YEAR" +
157+
" </rule>" +
158+
" <rule sensors = \"ANTXXXI-sic-cci, DMISIC0-sic-cci, DTUSIC1-sic-cci\">" +
159+
" insitu/sic-cci/SENSOR/VERSION" +
160+
" </rule>" +
161+
" </archive>" +
162+
" <temp-directory>" +
163+
" " + TestUtil.getTestDir().getAbsolutePath() +
164+
" </temp-directory>" +
165+
" <reader-cache-size>3</reader-cache-size>" +
166+
"</system-config>";
167167

168168

169169
final File systemConfigFile = new File(configDir, "system-config.xml");
@@ -176,21 +176,21 @@ public static void writeSystemConfig(File configDir, File archiveRoot) throws IO
176176

177177
public static void writeMmdWriterConfig(File configDir) throws IOException {
178178
final String config = "<mmd-writer-config>" +
179-
" <overwrite>false</overwrite>" +
180-
" <cache-size>2048</cache-size>" +
181-
" <netcdf-format>N4</netcdf-format>" +
182-
"</mmd-writer-config>";
179+
" <overwrite>false</overwrite>" +
180+
" <cache-size>2048</cache-size>" +
181+
" <netcdf-format>N4</netcdf-format>" +
182+
"</mmd-writer-config>";
183183

184184
writeMmdWriterConfigFile(configDir, config);
185185
}
186186

187187
public static void writeMmdWriterConfig(File configDir, String additionalTags) throws IOException {
188188
final String config = "<mmd-writer-config>" +
189-
" <overwrite>false</overwrite>" +
190-
" <cache-size>2048</cache-size>" +
191-
" <netcdf-format>N4</netcdf-format>" +
192-
additionalTags +
193-
"</mmd-writer-config>";
189+
" <overwrite>false</overwrite>" +
190+
" <cache-size>2048</cache-size>" +
191+
" <netcdf-format>N4</netcdf-format>" +
192+
additionalTags +
193+
"</mmd-writer-config>";
194194

195195
writeMmdWriterConfigFile(configDir, config);
196196
}
@@ -337,10 +337,10 @@ public static Matcher<String> matchesPattern(final String pattern) {
337337
public static Archive getArchive() throws IOException {
338338
final File dataDirectory = getTestDataDirectory();
339339
final String archiveXML = "<archive>" +
340-
" <root-path>" +
341-
dataDirectory +
342-
" </root-path>" +
343-
"</archive>";
340+
" <root-path>" +
341+
dataDirectory +
342+
" </root-path>" +
343+
"</archive>";
344344

345345
final ArchiveConfig archiveConfig = ArchiveConfig.parse(archiveXML);
346346
return new Archive(archiveConfig);

core/src/test/java/com/bc/fiduceo/core/SystemConfig_IO_Test.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import java.io.File;
3232
import java.io.IOException;
3333

34-
import static org.junit.Assert.assertEquals;
35-
import static org.junit.Assert.assertNotNull;
36-
import static org.junit.Assert.fail;
34+
import static org.junit.Assert.*;
3735

3836
@RunWith(IOTestRunner.class)
3937
public class SystemConfig_IO_Test {
@@ -61,7 +59,7 @@ public void testLoadFrom() throws IOException {
6159
assertNotNull(systemConfig);
6260

6361
assertEquals("S2", systemConfig.getGeometryLibraryType());
64-
assertEquals(12, systemConfig.getReaderCacheSize());
62+
assertEquals(3, systemConfig.getReaderCacheSize());
6563

6664
final ArchiveConfig archiveConfig = systemConfig.getArchiveConfig();
6765
assertEquals(TestUtil.getTestDataDirectory().getAbsolutePath(), archiveConfig.getRootPath().toString());

matchup-tool/src/test/java/com/bc/fiduceo/matchup/AbstractUsecaseIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void setUp() throws SQLException, IOException {
7070
TestUtil.writeDatabaseProperties_MongoDb(configDir);
7171
//TestUtil.writeDatabaseProperties_Postgres(configDir);
7272
TestUtil.writeSystemConfig(configDir);
73-
TestUtil.writeMmdWriterConfig(configDir);
73+
TestUtil.writeMmdWriterConfig(configDir, "<reader-cache-size>3</reader-cache-size>");
7474
}
7575

7676
@After

0 commit comments

Comments
 (0)