背景:
目标是使用OpenGL渲染数据编写相当大(至少2048 x 2048像素)的图像文件.
今天我首先使用glReadPixels将32位(argb8888)像素数据转换为int数组.
然后我将数据复制到一个新的短数组中,将32位argb值转换为16位(rgb565)值.此时我还将图像上下颠倒并更改颜色顺序,使opengl-image数据与android位图数据兼容(不同的行顺序和颜色通道顺序).
最后,我创建了一个Bitmap()实例和.copyPixelsFromBuffer(缓冲区b),以便能够将其作为png文件保存到磁盘.
但是我想更有效地使用内存,以避免某些手机上的内存崩溃.
题:
我可以以某种方式跳过int [] - > short []的第一个转换(并避免为像素数据分配新数组)吗?也许只使用字节数组/缓冲区并将转换后的像素写入我读取的同一个数组中......
更重要的是:我可以跳过位图创建(这里是程序崩溃的地方)并以某种方式将数据直接写入磁盘作为工作图像文件(并避免在位图对象中再次分配像素数据)?
编辑:如果我可以直接将数据写入文件,也许我不需要转换为16位像素数据,具体取决于文件大小以及稍后可以将文件读入内存的速度.
目前,我正在优化大型批处理程序的内存使用情况。最多的内存由不同的数据表使用。例如,我的DataTable dataTable使用大约260MB。
就像线程接受的答案中的建议“ 在.NET DataTable中存储数据的内存开销是多少? ”,我正在尝试将相关数据移出DataTable。这是我的代码:
GC.Collect(); // force the garbage collector to free memory
// First stop point - Total process memory (taskmanager) = 900 MB
List<ExpandoObject> expandoList = new List<ExpandoObject>();
foreach (DataRow dataRow in dataTable.Rows)
{
dynamic expandoItem = new ExpandoObject();
expandoItem.FieldName = dataRow["FieldName"].ToString();
expandoList.Add(expandoItem);
}
// Second stop point - Total process memory (taskmanager) = 1055 MB
dataTable.Clear();
dataTable.Dispose();
dataTable = null;
GC.Collect(); // force the garbage collector to free memory
// Third stop point - …Run Code Online (Sandbox Code Playgroud) 我在cuda中编写了一个应用程序,它在每个块中使用1kb的共享内存.由于每个SM中只有16kb的共享内存,所以整体上只能容纳16个块(我理解它是否正确?),虽然一次只能调度8个,但现在如果某个块忙于进行内存操作,所以其他块将在gpu上进行调度,但是所有共享内存都被已经在那里安排的其他16个块使用,所以cuda不会在同一个sm上安排更多的块,除非先前分配的块完全完成?或者它会将一些块的共享内存移动到全局内存,并在那里分配其他块(在这种情况下,我们应该担心全局内存访问延迟吗?)
由于布尔值实际占用1个字节的空间,因此a bool[]不是表示位数组的最节省空间的方式.有时整数和长整数用作更有效的位数组,但长整数和长整数只能容纳64位.是否有更节省空间的方法在有限的内存中存储数千万位的数组?
我需要对这个数组做的就是设置/清除各个位并检查某些位是1还是0,即我需要的唯一功能:
void Set(int index, bool value);
bool Get(int index);
Run Code Online (Sandbox Code Playgroud) 我来自Python / Ruby / JavaScript背景。我了解指针的工作原理,但是,我不确定在以下情况下如何利用指针。
假设我们有一个虚构的Web API,该API搜索一些图像数据库并返回一个JSON,该JSON描述在找到的每个图像中显示的内容:
[
{
"url": "https://c8.staticflickr.com/4/3707/11603200203_87810ddb43_o.jpg",
"description": "Ocean islands",
"tags": [
{"name":"ocean", "rank":1},
{"name":"water", "rank":2},
{"name":"blue", "rank":3},
{"name":"forest", "rank":4}
]
},
...
{
"url": "https://c3.staticflickr.com/1/48/164626048_edeca27ed7_o.jpg",
"description": "Bridge over river",
"tags": [
{"name":"bridge", "rank":1},
{"name":"river", "rank":2},
{"name":"water", "rank":3},
{"name":"forest", "rank":4}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我的目标是在Go中创建一个数据结构,该数据结构会将每个标签映射到如下所示的图像URL列表:
{
"ocean": [
"https://c8.staticflickr.com/4/3707/11603200203_87810ddb43_o.jpg"
],
"water": [
"https://c8.staticflickr.com/4/3707/11603200203_87810ddb43_o.jpg",
"https://c3.staticflickr.com/1/48/164626048_edeca27ed7_o.jpg"
],
"blue": [
"https://c8.staticflickr.com/4/3707/11603200203_87810ddb43_o.jpg"
],
"forest":[
"https://c8.staticflickr.com/4/3707/11603200203_87810ddb43_o.jpg",
"https://c3.staticflickr.com/1/48/164626048_edeca27ed7_o.jpg"
],
"bridge": [
"https://c3.staticflickr.com/1/48/164626048_edeca27ed7_o.jpg"
],
"river":[
"https://c3.staticflickr.com/1/48/164626048_edeca27ed7_o.jpg"
]
}
Run Code Online (Sandbox Code Playgroud)
如您所见,每个图像URL可以同时属于多个标签。如果我有成千上万个图像和更多标签,那么如果按每个标签的值复制图像URL字符串,则此数据结构会变得非常大。这是我要利用指针的地方。
我可以用Go中的两个结构来表示JSON API响应,func searchImages()模仿假API: …
我正在编写一个工匠控制台命令,它循环遍历表中的所有记录并重新生成该表上的字段.
该字段是a hash并且生成为md5()特定字符串的一个.
最初我的代码看起来像这样:
// Get all recipes
$recipes = Recipe::all();
$hashProgress = $this->output->createProgressBar(count($recipes));
// Loop over each recipe and generate a new hash for it
foreach ($recipes as $recipe)
{
$hashString = '';
$hashString .= $recipe->field1;
$hashString .= $recipe->field2;
$hashString .= $recipe->field3;
$hashString .= $recipe->field4;
$hashString .= $recipe->field5;
$hashString .= $recipe->field6;
$hashString .= $recipe->field7;
$extras1Total = $recipe->extras1->sum('amount');
$hashString .= $recipe->extras1->reduce(function ($str, $item) use ($extras1Total) {
return $str . $item->name . ($extras1Total == 0 ? $item->amount : …Run Code Online (Sandbox Code Playgroud) 将本地动态数组的长度设置为零(不再需要时)是否对内存使用有好处?
例如:
var
MyArray : array of string;
begin
<filling my array with a lot of items....>
<doing some stuffs with MyArray>
//from here on, MyArray is no more needed, should I set its length to zero?
SetLength(MyArray, 0);
<doing other stuffs which doesn't need MyArray...>
end;
Run Code Online (Sandbox Code Playgroud)