我最近在一些代码中遇到过这种情况 - 基本上有人试图创建一个大对象,当没有足够的堆来创建它时应对:
try {
// try to perform an operation using a huge in-memory array
byte[] massiveArray = new byte[BIG_NUMBER];
} catch (OutOfMemoryError oome) {
// perform the operation in some slower but less
// memory intensive way...
}
Run Code Online (Sandbox Code Playgroud)
这似乎不对,因为Sun自己建议您不要尝试捕获Error或其子类.我们讨论过它,另一个想法是显式检查空闲堆:
if (Runtime.getRuntime().freeMemory() > SOME_MEMORY) {
// quick memory-intensive approach
} else {
// slower, less demanding approach
}
Run Code Online (Sandbox Code Playgroud)
同样,这似乎并不令人满意 - 特别是因为选择一个值SOME_MEMORY很难轻易地与相关工作相关:对于某些任意大型对象,我如何估计其实例化可能需要多少内存?
有没有更好的方法呢?它甚至可以在Java中使用,还是在语言本身的抽象级别之下管理内存?
编辑1:在第一个例子中,估计byte[]给定长度的内存量可能实际上是可行的,但是有一种更通用的方法可以扩展到任意大对象吗?
编辑2:正如@erickson指出的那样,有一些方法可以在创建对象后估计对象的大小,但是(忽略基于先前对象大小的统计方法)是否有办法对尚未创建的对象执行此操作?
似乎还有一些争论是否合理OutOfMemoryError- 有人知道任何结论吗?
简单的问题,在HTML 4.0 Strict和XHTML 1.0 Strict中删除目标属性的刺激下.
我正在尝试使用以下代码将匿名类型序列化为JSON:
var serializer = new DataContractJsonSerializer(thing.GetType());
var ms = new MemoryStream();
serializer.WriteObject(ms, thing);
var json = Encoding.Default.GetString(ms.ToArray());
Run Code Online (Sandbox Code Playgroud)
但是,执行此操作时会出现以下异常:
类型'<> f__AnonymousType1`3 [System.Int32,System.Int32,System.Object []]'无法序列化.请考虑使用DataContractAttribute属性对其进行标记,并使用DataMemberAttribute属性标记要序列化的所有成员.有关其他受支持的类型,请参阅Microsoft .NET Framework文档.
我不能将属性应用于匿名类型(据我所知).有没有其他方法来进行此序列化或我错过了什么?
c# json anonymous-types json-serialization datacontractjsonserializer
我的列表(@degree)是从SQL命令构建的.SQL中的NVL命令不起作用,测试如下:
if (@degree[$i] == "")
if (@degree[$i] == " ")
if (@degree[$i] == '')
if (@degree[$i] == -1)
if (@degree[$i] == 0)
if (@degree[$i] == ())
if (@degree[$i] == undef)
Run Code Online (Sandbox Code Playgroud)
$ i是for循环中的计数器变量.基本上它通过并从表中获取独特的度数并最终创建("AFA", "AS", "AAS", "", "BS").列表并不总是很长,空元素并不总是在那个位置3.
有人可以帮忙吗?
我想要在for循环期间进行测试,或者在循环完成后对这个空元素进行测试,然后将其替换为单词"OTHER".
谢谢你的一切 - 肯
我在同一个UITableView中插入和删除UITableViewCells时遇到了很多麻烦!
我通常不发布代码,但我认为这是显示我遇到问题的最佳方式:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (iSelectedSection == section) return 5;
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"drawing row:%d section:%d", [indexPath row], [indexPath section]);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if (iSelectedSection == [indexPath section]) {
cell.textColor = [UIColor redColor];
} else {
cell.textColor = [UIColor blackColor];
}
cell.text = …Run Code Online (Sandbox Code Playgroud) 有什么用途:
static_castdynamic_castconst_castreinterpret_cast(type)valuetype(value)如何决定在哪些特定情况下使用哪个?
我一直在研究Scott Guthrie关于ASP.NET MVC Beta 1的优秀帖子.在其中,他展示了对UpdateModel方法所做的改进以及它们如何改进单元测试.我已经重新创建了一个类似的项目,但是当我运行包含对UpdateModel的调用的UnitTest时,我会收到一个名为controllerContext参数的ArgumentNullException.
这是相关的位,从我的模型开始:
public class Country {
public Int32 ID { get; set; }
public String Name { get; set; }
public String Iso3166 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器动作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Int32 id, FormCollection form)
{
using ( ModelBindingDataContext db = new ModelBindingDataContext() ) {
Country country = db.Countries.Where(c => c.CountryID == id).SingleOrDefault();
try {
UpdateModel(country, form);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch {
return View(country);
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后我的单元测试失败了:
[TestMethod]
public void Edit() …Run Code Online (Sandbox Code Playgroud) asp.net-mvc updatemodel argumentnullexception controllercontext
我正在使用一些示例java代码来制作md5哈希.一部分将结果从字节转换为十六进制数字的字符串:
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<messageDigest.length;i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
}
Run Code Online (Sandbox Code Playgroud)
但是,由于toHexString显然会从前导零中掉落,所以它并不常用.那么,从字节数组到保持前导零的十六进制字符串的最简单方法是什么?
我有一个网站,它使用通用的mod_rewrite规则将所有请求推送到index.php页面,但某些文件扩展名除外:
RewriteRule !\.(js|ico|gif|jpg|JPG|png|css|php|phtml|pdf|txt|xml)$ index.php
Run Code Online (Sandbox Code Playgroud)
我需要做的是从该规则中排除某个目录(包括其中包含的任何文件或子目录) - 最佳解决方案是什么?
这是我的完整.htaccess文件,以防其中的其他内容正在进行:
RewriteEngine ON
RewriteCond %{HTTP_HOST} !^www\..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*)\.(co\.uk)
RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=permanent,L]
AddHandler application/x-httpd-php .phtml
RewriteRule !\.(js|ico|gif|jpg|JPG|png|css|php|phtml|pdf|txt|xml)$ index.phtml
php_value display_errors "On"
Run Code Online (Sandbox Code Playgroud) 我需要将double存储为字符串.我知道我可以使用,printf如果我想显示它,但我只是想将它存储在一个字符串变量中,以便我以后可以将它存储在地图中(作为值,而不是键).