# Filter is doubling allocated memory (JAVA)

**URL:** <https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144>\
**Category:** Beginner Questions\
**Created:** [August 11, 2023, 6:08pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144 "2023-08-11T18:08:55Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 11, 2023, 6:08pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/1 "2023-08-11T18:08:56Z")

</div>

my tests are being done with a series that has approximately 118MB.  
 ![image](https://discourse.itk.org/uploads/default/original/2X/c/c7065a92344a1647f3a384e97a695a1515dcd01c.png)

memory consumption of my software just by startup:  
 ![image](https://discourse.itk.org/uploads/default/original/2X/4/4ff602e555065df6464d602157c57217f2bd3c50.png)

after loading the series:

```auto
this.reader.setFileNames(this.dicomFilesNames);
this.series = reader.execute();

```

![image](https://discourse.itk.org/uploads/default/original/2X/e/eb4e1adc63704a801a43228c1bc29020383cb743.png)

finally, after running the final filter:

```auto
private void fixOrientation () {
        DICOMOrientImageFilter filter = new DICOMOrientImageFilter();
        switch (this.type) {
            case SCALAR -> filter.setDesiredCoordinateOrientation("RAI");
            case VECTOR -> filter.setDesiredCoordinateOrientation("LPI");
        }
        this.series = filter.execute(this.series);
    }

```

![image](https://discourse.itk.org/uploads/default/original/2X/c/c9f24cea6fc802d24df4c49ac1283af7dc33fefa.png)

Can anyone explain how exactly simpleITK works in this situation and what can i do to solve the problem?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [August 11, 2023, 7:07pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/2 "2023-08-11T19:07:57Z")

</div>

Hello @Salu_Ramos,

This looks like Java behavior not specific to SimpleITK. In the line:

```auto
this.series = filter.execute(this.series);

```

the variable `this.series` now points to a new memory location and the old one is still around waiting to be garbage collected (assuming no-one else is pointing at that memory location). You can request collection using [System.gc()](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/System.html#gc()), but this is just a request and the memory may not be released till later on.

Possibly someone in the community with more Java expertise can comment more about Java memory management.

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 11, 2023, 9:28pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/3 "2023-08-11T21:28:32Z")

</div>

Hi @zivy,

even using System.gc() right after the filter, memory continues basically the same.

A question, was it expected that a series that occupies 118MB in disk would occupy 570MB in memory?  
I mean, I don’t understand why it takes up almost 6 times more space.

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [August 12, 2023, 12:35am UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/4 "2023-08-12T00:35:58Z")

</div>

Hi @Salu_Ramos,

As I said and as the documentation says, calling `System.gc()` is requesting/hinting to Java to invoke the garbage collector, it isn’t directly invoking it. I don’t know if there is a way to force immediate garbage collection which is why I asked for input from the community.

This is more of a Java issue, so possibly ask on in a forum dedicated to the language.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [August 14, 2023, 1:01pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/5 "2023-08-14T13:01:52Z")

</div>

Also the SimpleITK library is a native library for JAVA, and that adds an additional layer of complexity to the memory management.

This article looks interesting:

> **[understanding java native memory](http://www.trevorsimonton.com/blog/2020/09/09/java-native-memory.html)**
>
> Recently when working on a Java application that used a lotof JNI code, I found myself asking a lot of questions abouthow memory is managed in the JNI layer....

How are you measuring memory allocation for the application? Just because the application has memory allocated does not mean that it is physically occupying the memory on the computer. This can happen when large libraries are mapped to memory and not physically loaded into memory ( SimpleITK is a large library ).

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 15, 2023, 1:22am UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/6 "2023-08-15T01:22:53Z")

</div>

> [@blowekamp](#):
>
> How are you measuring memory allocation for the application?

i was using task manager, hehe.  
Now i tried JProfiler with intellij idea.

 ![image](https://discourse.itk.org/uploads/default/original/2X/b/bc55c5bb0da8ffba2e83959558a97eda2a0198cf.jpeg)

The result of JProfiler are way too different from task manager and  
resource monitor.  
I think the JProfiler is not considering the jni lib heap memory (.dll).

task manager:

 ![image](https://discourse.itk.org/uploads/default/original/2X/e/e81b87ecce0090fbca3ac773592b406585ca8308.png)

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 15, 2023, 6:32pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/7 "2023-08-15T18:32:46Z")

</div>

Hi @zivy, i understand that **System.gc()** does not guarantee the release of ram memory.

> [@Salu\_Ramos](#):
>
> A question, was it expected that a series that occupies 118MB in disk would occupy 570MB in memory?

but this question still in my head.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [August 15, 2023, 7:00pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/8 "2023-08-15T19:00:02Z")

</div>

To estimate the size of the image in memory, it is the number of pixels multiplied by the size of the pixel type. What are these values and your results?

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 16, 2023, 4:49pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/9 "2023-08-16T16:49:54Z")

</div>

the series i’m using is a uint32 pixeltype.  
the size is 512x512x549.

so, 4bytes \* 512 \* 512 \* 549 = 575.668.224 bytes

which is ~575 MB, now that make’s sense to me.  
i thought itk used some kind of compression system.

Anyway, that means I can save memory space by converting the image to an 8-bit pixel type, cool.

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 17, 2023, 3:49am UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/10 "2023-08-17T03:49:10Z")

</div>

I found a method in **ImageSeriesReader** called **setOutputPixelType** that makes exactly what i need.

my code:

```auto
this.reader.setOutputPixelType(mainPixelID.getItkLightVersionID());

```

the method **getItkLightVersionID** is a enum method that returns de pixelID light version, basically map the higher bits version to the lower 8 bits version, keeping the sign (signed or unsigned) and type (scalar, vector or label).

 ![image](https://discourse.itk.org/uploads/default/original/2X/f/fc7d0002366b8518c5522b573ec7aafd187c82b3.png)

example:  
uint32 returns uint8  
uint16 returns uint8  
int16 returns int8  
vectoruint64 returns vectoruint8

but my final result isn’t right.

result:

 ![image](https://discourse.itk.org/uploads/default/original/2X/8/8bb5ac444b764f99e5fe1b37a4bacad6f06a6023.jpeg)

expected:

 ![image](https://discourse.itk.org/uploads/default/original/2X/e/e6c16e38749f517ac73fa3f75321b062252e1aab.jpeg)

do you have any idea why is this happening?

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [August 17, 2023, 1:18pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/11 "2023-08-17T13:18:17Z")

</div>

Hello @Salu_Ramos,

The method you are using is simply coercing the values into uint8 so values that were originally negative and values that were greater than 255 are not represented correctly. You can read the image with the original intensity range, [linearly rescale the intensities](https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1RescaleIntensityImageFilter.html) to [0,255] and then cast to uint8 and the image will look OK, but you will be loosing information that was contained in the original image.

There is no magic, you cannot directly fit the same amount of information into less memory (this isn’t a compressed representation of the values).

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 17, 2023, 7:52pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/12 "2023-08-17T19:52:21Z")

</div>

Thanks @zivy,

I managed to reproduce what you told me but it’s still not 100%, some layers are too white, see the layer scroll function and the jump in brightness. This doesn’t happen in other viewers even using default values ​​for wcww.

[![]( " - YouTube") ](https://www.youtube.com/watch?v=xqSPZTHdk_M)

code:

```auto
this.reader.setFileNames(this.dicomFilesNames);
this.series = reader.execute();

RescaleIntensityImageFilter rescaleFilter = new RescaleIntensityImageFilter();
rescaleFilter.setOutputMinimum(0);
rescaleFilter.setOutputMaximum(255);
this.series = rescaleFilter.execute(this.series);

CastImageFilter castFilter = new CastImageFilter();
castFilter.setOutputPixelType(PixelIDValueEnum.sitkUInt8);
this.series = castFilter.execute(this.series);

```

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [August 18, 2023, 12:55pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/13 "2023-08-18T12:55:47Z")

</div>

Video unavailable  
This video is private

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 18, 2023, 1:46pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/14 "2023-08-18T13:46:58Z")

</div>

I changed the video to public

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [August 18, 2023, 1:58pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/15 "2023-08-18T13:58:04Z")

</div>

If you do per-slice rescaling, instead of per-volume rescaling, you get those jumpy intensities from slice to slice.

---

<div class="post-metadata">

**Author:** ![Salu\_Ramos](https://discourse.itk.org/user_avatar/discourse.itk.org/salu_ramos/32/3619_2.png) [@Salu\_Ramos](https://discourse.itk.org/u/Salu_Ramos)\
**Post date:** [August 18, 2023, 7:36pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/16 "2023-08-18T19:36:04Z")

</div>

i’m rescaling only once the entire series.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [August 18, 2023, 7:50pm UTC](https://discourse.itk.org/t/filter-is-doubling-allocated-memory-java/6144/17 "2023-08-18T19:50:16Z")

</div>

Maybe some slices are missing rescale slope/intercept tags? Or just a few slices have them?
